Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

php - Upload multiple images and store their path in database

I am making a form through which user can upload multiple images. When the user uploads the images they get stored in the server's folder. Everything works fine till here but when I am trying to save the path of the images in the database then instead of the path, the name of both the images gets stored in one row only. I want that the path of each image should get stored in different row.

<form action="admin_insert_property_images.php" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label class="col-md-3 control-label">Upload Image:</label>
            <div class="col-md-8">
                <input type="file" id="file" name="support_images[]" multiple accept="image/*" />
            </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label"></label>
            <div class="submit">
                <input class="btn btn-primary" value="Save " type="submit" name="submit">
            </div>  
    </div>
</form>

admin_insert_property_images.php

<?php
$con=mysqli_connect("abc.com","abc","ab","abc");
// Check connection
if (mysqli_connect_errno()) 
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

if(isset($_POST['submit']))           
{
 extract($_POST);

    if(isset($_FILES['support_images']['name']))
    {
        $file_name_all="";
        for($i=0; $i<count($_FILES['support_images']['name']); $i++) 
        {
               $tmpFilePath = $_FILES['support_images']['tmp_name'][$i];    
               if ($tmpFilePath != "")
               {    
                   $path = "propertyimages/"; // create folder 
                   $name = $_FILES['support_images']['name'][$i];
                  $size = $_FILES['support_images']['size'][$i];

                   list($txt, $ext) = explode(".", $name);
                   $file= time().substr(str_replace(" ", "_", $txt), 0);
                   $info = pathinfo($file);
                   $filename = $file.".".$ext;
                   if(move_uploaded_file($_FILES['support_images']['tmp_name'][$i], $path.$filename)) 
                   { 
                      $file_name_all.=$filename."*";
                   }
             }
        }
        $filepath = rtrim($file_name_all, '*'); 
$query=mysqli_query($con,"INSERT into propertyimages (`propertyimage`) VALUES('".addslashes($filepath)."'); ");    
        }
        else
    {
        $filepath="";
    }

    if($query)
    {
       header("Location: admin_profile.php");
    }
}
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your $filepath variable and your query has to be in your loop.

You are also using mysql_query which is not compatible with mysqli_ functions.

Those two APIs do not mix together. Use mysqli_query while passing DB connection to it.

<?php
$con=mysqli_connect("abc.com","abc","ab","abc");
// Check connection
if (mysqli_connect_errno()) 
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

if(isset($_POST['submit']))           
{
 extract($_POST);

    if(isset($_FILES['support_images']['name']))
    {
        $file_name_all="";
        for($i=0; $i<count($_FILES['support_images']['name']); $i++) 
        {
               $tmpFilePath = $_FILES['support_images']['tmp_name'][$i];    
               if ($tmpFilePath != "")
               {    
                   $path = "propertyimages/"; // create folder 
                   $name = $_FILES['support_images']['name'][$i];
                  $size = $_FILES['support_images']['size'][$i];

                   list($txt, $ext) = explode(".", $name);
                   $file= time().substr(str_replace(" ", "_", $txt), 0);
                   $info = pathinfo($file);
                   $filename = $file.".".$ext;
                   if(move_uploaded_file($_FILES['support_images']['tmp_name'][$i], $path.$filename)) 
                   { 
                      $file_name_all.=$filename."*";
                   }
             }
              $filepath = rtrim($file_name_all, '*').$path;    
         $query=mysqli_query($con,"INSERT into propertyimages (`propertyimage`) VALUES('".addslashes($filepath)."'); ");
        }

    }
    else
    {
        $filepath="";
    }

    if($query)
    {
       header("Location: admin_profile.php");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...