将图像路径保存在数据库中

时间:2015-01-14 08:59:24

标签: php mysql sql mysqli

下面的代码有助于上传图像,但我希望将图像路径保存在数据库中。

目前代码是

    <?php

        if (!isset($_FILES["item_file"]))
            die ("Error: no files uploaded!");

        $file_count = count($_FILES["item_file"]['name']);

        //echo $file_count . " file(s) sent... <BR><BR>";

        if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded

            for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array

                $filen = $_FILES["item_file"]['name'][$j];  

                // ingore empty input fields
                if ($filen!="")
                {

                    // destination path - you can choose any file name here (e.g. random)
                    $path = "upload/" . $filen; 

                    if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { 

                        //echo "File# ".($j+1)." ($filen) uploaded successfully!<br>"; 
                        echo $path;

                    } else
                    {
                        echo  "Errors occoured during file upload!";
                    }
                }   

            }
        }


?>

我已经按echo $path打印了所有图片的路径。我得到的结果是

upload/1.jpgupload/2.jpgupload/3.jpg

我希望在变量中单独保存每个路径,然后在表中更新。这样的事情会很棒

$image1=upload/1.jpg
$image2=upload/2.jpg
$image3=upload/3.jpg

然后我可以更新它们

$sql = "UPDATE tablename SET image1='$image1',image2='$image2',image3='$image3' WHERE id=1";

并且表格看起来应该是这样的

id          image1           image2             image3
1         upload/1.jpg     uploade/2.jpg      upload/3.jpg

任何人都可以告诉我如何做到这一点

4 个答案:

答案 0 :(得分:1)

在循环开始后创建一个图像键:

$imageKey = $j+1:

你可以在echo $ path之后将你的pathes存储在一个数组中:

$image[$imageKey] = $path;
循环后,您可以使用此查询:

$sql = "UPDATE tablename SET image1='".$image[1]."',image2='".$image[2]."',image3='".$image[3]."'
WHERE id=1";

答案 1 :(得分:0)

尝试将它们保存为数组 -

$uploads = array();
//... codes
     if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) {
        //echo "File# ".($j+1)." ($filen) uploaded successfully!<br>"; 
        //echo $path;
        $uploads[] = $path;
     }
//... rest of the code

并在查询中 -

$sql = "UPDATE tablename SET 
       image1='".!empty($uploads[0]) ? $uploads[0] : ""."',
       image2='".!empty($uploads[1]) ? $uploads[1] : ""."',
       image3='".!empty($uploads[2]) ? $uploads[2] : ""."'' 
       WHERE id=1";

答案 2 :(得分:0)

不是存储在$path变量中,而是使用数组$path[$j]。不要忘记在开头将$ path声明为空array()

之后你可以运行sql as

$sql = "UPDATE tablename SET image1='$path[0]',image2='$path[1]',image3='$path[3]' WHERE id=1";

答案 3 :(得分:0)

试试这个:

$path_list= array();
//... codes
     if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) {
        //echo "File# ".($j+1)." ($filen) uploaded successfully!<br>"; 
        //echo $path;
        $path_list[] = $path;
     }
//... rest of the code

和sql

$sql = "UPDATE tablename SET image1='".@$path_list[1]."',image2='".@$path_list[2]."',image3='".@$path_list[3]."'  WHERE id=1";