Php 将映像的路径存储在不同的数据库列中

Php 将映像的路径存储在不同的数据库列中,php,mysql,sql,sql-server,mysqli,Php,Mysql,Sql,Sql Server,Mysqli,我有一个表单,可以帮助将多个图像上载到服务器文件夹并将其路径保存在数据库中,问题是所有图像的路径都上载到一列,因此我在获取路径以显示图像时遇到问题。解决方案是,我要么将图像存储在不同的列中,要么找到分离图像的方法,但我无法同时做到这两个 存储的路径如下所示 uploads/c376437e2a45598b2f4d89eae4f191e8.png*uploads/c376437e2a45598b2f4d89eae4f191e8.png8069756be5095978123ae51fadbffe3b

我有一个表单,可以帮助将多个图像上载到服务器文件夹并将其路径保存在数据库中,问题是所有图像的路径都上载到一列,因此我在获取路径以显示图像时遇到问题。解决方案是,我要么将图像存储在不同的列中,要么找到分离图像的方法,但我无法同时做到这两个

存储的路径如下所示

uploads/c376437e2a45598b2f4d89eae4f191e8.png*uploads/c376437e2a45598b2f4d89eae4f191e8.png8069756be5095978123ae51fadbffe3b.png*uploads/c376437e2a45598b2f4d89eae4f191e8.png8069756be5095978123ae51fadbffe3b.png04aaa414c21dc057bc594b896124068e.png
数据库结构演示

id  offimage offimage1   offimage2
到目前为止我拥有的代码(表单和php代码在不同的页面上)


最后我得到了解决方案,我看到一些人已经标记了这个问题,他们可能正在寻找答案,所以就在这里

首先连接到数据库,获取数据,然后使用以下代码

while($row1 = mysqli_fetch_assoc($result1)) 
    {
    $str= $row1["offimage"];
    //print_r (explode("*",$str));

    $array =  explode('*', $str);
    foreach ($array as $item) {
        //echo "<li>$item</li>";
        echo "<img src=\"http://localhost/cms/1/". $item . "\" height=\"200\" width=\"200\"/>";
    }

}
while($row1=mysqli\u fetch\u assoc($result1))
{
$str=$row1[“offimage”];
//打印(分解(“*”,$str));
$array=explode('*',$str);
foreach($数组作为$项){
//回声“
  • $item
  • ”; 回声“; } }
    如何创建另一个包含父级索引的表,然后将图像存储在该表中?@Anand Ghaywankar仍然将所有图像路径存储在一列中,如何存储在一列中?一个diff表将具有col-like-id、parent\u-id、img\u-path,并且它将与parent\u表有许多关系。您只需在映像中维护父表Idtable@AnandGhaywankarr是的,我有一个名为img_path(正如你提到的)的路径,我给出的路径存储在img_path下,为什么你要更新行而不是插入一个新的?
    <?php
    ob_start();
    include('co_session.php');
    $con=mysqli_connect("localhost","root","","db");
    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    if (isset($_POST['submit'])) {
        $j = 0; //Variable for indexing uploaded image 
    
        $target_path = "uploads/"; //Declaring Path for uploaded images
        for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
    
            $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
            $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
            $file_extension = end($ext); //store extensions in the variable
    
            $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
            $j = $j + 1;//increment the number of uploaded images according to the files in array       
    
          if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
                    && in_array($file_extension, $validextensions)) {
                if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                    //echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
    
                    $file_name_all.=$target_path."*";
                    $filepath = rtrim($file_name_all, '*'); 
                    //echo $filepath;
                    $officeid = $_GET['id'];
                    $sql = "UPDATE register_office SET offimage='$filepath' WHERE id='$officeid' ";
                                if (!mysqli_query($con,$sql)) 
                                    {
                                        die('Error: ' . mysqli_error($con));
                                    }
    
                } else {//if file was not moved.
                    echo $j. ').<span id="error">please try again!.</span><br/><br/>';
                }
            } else {//if file size and file type was incorrect.
                echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
            }
        }
        header("Location: co_request_sent.php ");
    }
    mysqli_close($con); 
    ?>
    
    while($row1 = mysqli_fetch_assoc($result1)) 
        {
        $str= $row1["offimage"];
        //print_r (explode("*",$str));
    
        $array =  explode('*', $str);
        foreach ($array as $item) {
            //echo "<li>$item</li>";
            echo "<img src=\"http://localhost/cms/1/". $item . "\" height=\"200\" width=\"200\"/>";
        }
    
    }