Jump to content

Uploading more than one image?


Stevis2002

Recommended Posts

Hi all,

 

I have the following script which uploads 1 image per testimonial, and stores the url in the database, in a field named 'Images'.

I have added more upload fields to my form and named them images 2,images 3 etc, and then i tried to copy the following code to upload the urls in to the extra fields i mage for the urls in the database, ('Images2','Images3' etc etc), but i am getting a blank page now. I even copied teh function and adjusted the names...eg $imgname2, 3 etc.

Can anybody help me please?

Here is the code that deals with the image upload

 

Thanks

 

<p align="center"> </p>
</body>
<?php
$con = mysql_connect("localhost","xxxxxxxxxxxxxxxxx","xxxxxxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xxxxxxxxxxxx", $con);

$image_tmpname = $_FILES['images']['name'];
$imgdir = "uploaded_images/";
$imgname = $imgdir.$image_tmpname;

if(move_uploaded_file($_FILES['images']['tmp_name'], $imgname))
{
list($width,$height,$type,$attr)= getimagesize($imgname);
switch($type)
{
case 1:
  $ext = ".gif"; break;
case 2:
  $ext = ".jpg"; break;
case 3:
  $ext = ".png"; break;
default:
   echo "Not acceptable format of image";
}

$sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images)
VALUES
('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')";

}
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<p align=center><b>1 testimonial added</b></p>";

mysql_close($con);
?>
</html>

Link to comment
https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/
Share on other sites

Thanks for that, but now i get nothing entering the database, just an error saying that the query was empty :(

 

<form action="add_testimonials.php" method="post" enctype="multipart/form-data" name="add_test" id="add_test">
  <p> </p>
  <p align="center">
    <label for="customername">Customer Name:</label>
    <input name="customername" type="text" id="customername" maxlength="150" />
  </p>
  <p align="center">
    <label for="town">Town/City:   </label>
    <input name="town" type="text" id="town" maxlength="150" />
  </p>
  <p align="center">
    <label for="testimonial"><u>Testimonial </u></label>
  </p>
  <p align="center">
    <textarea name="testimonial" id="testimonial" cols="60" rows="10"></textarea>
  </p>
  <p align="center">
    <label for="sort_order">Sort Order: </label>
    <input name="sort_order" type="text" id="sort_order" size="10" maxlength="3" />
  </p>
  <p align="center">
    <label for="images">Upload Image</label>
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
  </p>
  
  <p align="center">
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
  <p> </p>
  <p> </p>
</form>
<p align="center"> </p>
</body>
<?php
$con = mysql_connect("localhost","xxxxxxxxxxxxxxxxx","xxxxxxxxxxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xxxxxxxxxxxxx", $con);

foreach ($_FILES["images"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["images"]["tmp_name"][$key];
        $name = $_FILES["images"]["name"][$key];
        move_uploaded_file($tmp_name, "data/$name");
    }
}


//$image_tmpname = $_FILES['images']['name'];
$imgdir = "uploaded_images/";
//$imgname = $imgdir.$image_tmpname;

if(move_uploaded_file($_FILES['images']['tmp_name'], $imgname))
{
list($width,$height,$type,$attr)= getimagesize($imgname);
switch($type)
{
case 1:
  $ext = ".gif"; break;
case 2:
  $ext = ".jpg"; break;
case 3:
  $ext = ".png"; break;
default:
   echo "Not acceptable format of image";
}

$sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images)
VALUES
('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')";

}
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<p align=center><b>1 testimonial added</b></p>";

mysql_close($con);
?>

You would need to adapt your code that processes an uploaded image and put it inside the foreach(){... code that is executed for-each uploaded file ...} loop.

 

And your query empty error is because you are setting the $sql variable inside of a conditional statement that is evaluating as FALSE, while your code that uses $sql is being executed unconditionally and $sql variable is empty, ergo the query is empty error.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.