...

View Full Version : Uploading multiple images with a form to server and storing file name in MySQL



natie769
03-08-2009, 08:49 PM
I have a form that allows the user to upload multiple images (5 max). This code works for me for uploading one image, but it doesn't work now that I'm trying to upload multiple images.

It uploads the file names to the MySQL table perfectly, but it doesn't upload the image to the server. Can anyone tell me where I've gone wrong -- thanks!!! :)
I'm assuming its something small I'm missing since it worked for uploading just one image. ;)

Here is my form:



<form action="nextpage.php" method="post" enctype="multipart/form-data">

<table>
<tr>
<td valign="top">Upload Image: </td>

<td>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo" type="file" size="30"><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo2" type="file" size="30"><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo3" type="file" size="30"><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo4" type="file" size="30"><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo5" type="file" size="30"></td>
</tr>
</table>


Here is the nextpage.php code:



<?php
$photo=($_FILES['photo']['name']);
$photo2=($_FILES['photo2']['name']);
$photo3=($_FILES['photo3']['name']);
$photo4=($_FILES['photo4']['name']);
$photo5=($_FILES['photo5']['name']);

//This is the directory where images will be saved
$target = "images/uploaded/";
$target = $target . basename( $_FILES['photo']['name']['photo2']['name']['photo3']['name']['photo4']['name']['photo5']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name']['photo2']['tmp_name']['photo3']['tmp_name']['photo4']['tmp_name']['photo5']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}

$database info

mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="INSERT INTO `Listings` (`photo`, `photo2`, `photo3`, `photo4`, `photo5`) VALUES('".$photo."', '".$photo2."', '".$photo3."', '".$photo4."', '".$photo5."')";
$result=mysql_query($sql);

riwan
03-09-2009, 05:36 AM
You can't just upload all the 5 images using one line.
Do them separately



move_uploaded_file($_FILES['photo']['tmp_name']['photo2']['tmp_name']['photo3']['tmp_name']['photo4']['tmp_name']['photo5']['tmp_name'], $target)

natie769
03-09-2009, 03:47 PM
How would I write that?

Like this?:



if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
if(move_uploaded_file($_FILES['photo2']['tmp_name'], $target))
if(move_uploaded_file($_FILES['photo3']['tmp_name'], $target))
if(move_uploaded_file($_FILES['photo4']['tmp_name'], $target))
if(move_uploaded_file($_FILES['photo5']['tmp_name'], $target))
{


Do I also have to change this line?



$target = $target . basename( $_FILES['photo']['name']['photo2']['name']['photo3']['name']['photo4']['name']['photo5']['name']);



Thanks!

riwan
03-09-2009, 05:06 PM
do it like this :




$target = "images/uploaded/";
$targetx = $target . basename( $_FILES['photo']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo2']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo2']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo3']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo3']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo4']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo4']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo5']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo5']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

natie769
03-10-2009, 02:41 AM
That worked perfectly -- thank you so much!! :)
You're awesome!:thumbsup:

inebula88
04-09-2009, 04:24 PM
This thread has helped me a ton. Thank you!! My code is 'almost' working. I am getting this error:
Error: Column count doesn't match value count at row 1

I have counted the insert and MySQL table and I think I have the same everywhere so am not sure why I'm getting this error. I am posting all my related code below and would appreciate any suggestions. THANK YOU!

HERE IS THE HTML:
<form action="insertTEST.php" method="post" target="_top" enctype="multipart/form-data" name="modelinquiry" id="modelinquiry" onSubmit="MM_validateForm('firstname','','R', 'email','','RisEmail');return document.MM_returnValue">
<p>Name:
<input type="text" name="firstname" size="65"/>

</p>
<p> Email:
<input type="text" name="email" size="50" />
<br />
<span class="red"><br />

OPTIONAL BUT HELPFUL:</span><br />
<br />

Age:
<input type="text" name="age" size="12"/>
<br />
<br />
What is your state of residence?
<input type="text" name="state" size="15" />
<br />
<br />

Are you willing to pose nude?
<input type="radio" name="nude" value="yes" />
Yes
<input type="radio" name="nude" value="no" />
No <br />
<br />

Do you have any modeling/acting experience?
<input type="radio" name="acting" value="yes" />
Yes
<input type="radio" name="acting" value="no" />
No </p>

<p><br />IMAGES<BR>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo" type="file" size="30"><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo2" type="file" size="30"><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo3" type="file" size="30"><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo4" type="file" size="30"><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="photo5" type="file" size="30">

<br />
Comments/Questions:<br>
<textarea name="comments" rows="4" cols="60"></textarea>
<br />
<br />


<input type="submit" value="Submit"/>
(Privacy & Confidentially Assured)
<input type="reset" name="Reset" value=" Reset ">
<img src="images/model_form.jpg" width="316" height="211" align="right"></p>
</form> </TD>

HERE IS THE PHP
<title>insert</title>
<?php
$con = mysql_connect("localhost","USERNAME","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db('MYDB');



$sql="INSERT INTO Persons (FirstName, email, Age, state, nude, acting, comments, photo, photo2, photo3, photo4, photo5)
VALUES
('$_POST[firstname]', '$_POST[email]', '$_POST[age]', '$_POST[state]', '$_POST[photo]' , '$_POST[nude]' , '$_POST[acting]' , '$_POST[comments]', '$_FILES[photo]' ,'$_FILES[photo2]' , '$_FILES[photo3]' ,'$_FILES[photo4]' ,'$_FILES[photo5]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
?>
<script language="JavaScript" type="text/JavaScript">
<!--
window.location.href = "http://www.cash4fetishmodels.com/formsubmitted.htm";
//-->
</script>
<?php

$first_name=$_POST['firstname'];
$last_name=$_POST['lastname'];
$email=$_POST['email'];
$Age=$_POST['Age'];
$state=$_POST['state'];
$photo=$_POST['photo'];
$nude=$_POST['nude'];
$acting=$_POST['acting'];
$comments=$_POST['comments'];

$photo=($_FILES['photo']['name']);
$photo2=($_FILES['photo2']['name']);
$photo3=($_FILES['photo3']['name']);
$photo4=($_FILES['photo4']['name']);
$photo5=($_FILES['photo5']['name']);

//This is the directory where images will be saved
$target = "images/uploaded/";
$targetx = $target . basename( $_FILES['photo']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo2']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo2']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo3']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo3']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo4']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo4']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo5']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo5']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}


$to = "modelinquiry@cash4fetishmodels.com";
$from_header = "From: modelinquiry@cash4fetishmodels.com";
$subject = "Model Request";
$contents = <<< emailbody
Dear Webmaster,

This person has requested more information:
First Name=$first_name
Email=$email
Age=$Age
State=$state
Photo=$photo
Nude=$nude
Acting=$acting
Comments=$comments



emailbody;
mail($to, $subject, $contents, $from_header);
mysql_close($con);
//echo header ("Location: http://cash4fetishmodels.com/formsubmitted.htm");

?>

Shinykirby
04-09-2009, 09:44 PM
To the above - please use [code] tags next time to make posting a bit easier to read. Secondly, don't hijack a thread (and especially one that you pretty much copied the code from), make your own topic.

Anyway, this is your problem:

$sql="INSERT INTO Persons (FirstName, email, Age, state, nude, acting, comments, photo, photo2, photo3, photo4, photo5)
VALUES
('$_POST[firstname]', '$_POST[email]', '$_POST[age]', '$_POST[state]', '$_POST[photo]' , '$_POST[nude]' , '$_POST[acting]' , '$_POST[comments]', '$_FILES[photo]' ,'$_FILES[photo2]' , '$_FILES[photo3]' ,'$_FILES[photo4]' ,'$_FILES[photo5]')";

You're missing a " '$_POST[nude]' " in the VALUES section after the " '$_POST[state]' ". Also, you should never do anything with $_POST until you are absolutely positive the value is set!

guelphdad
04-10-2009, 03:07 AM
also please don't post an entire script when it isn't relevant. post only the part of the code where you are getting error problems (i.e. the insert statement).

you'll also want to look at DATABASE NORMALIZATION, spend your time fixing your data and table structures now before it's got to the point where all your code is working so you'll end up muddling through with crappy data structure.

inebula88
04-15-2009, 07:09 PM
I wanted to thank all you guy in this thread. You have been a big help. I still have a few questions but have been asked to start my own topic so will do so. I appreciate the kind advice on protocol and patience with a newbie!!

bazz
04-16-2009, 02:02 AM
... but have been asked to start my own topic so will do so.

I am sure the rules explain it better ;) but, basically, you should start a thread for each specific thing you need help with. no point in asking for help with php and html in the same thread since they need to be in different forums.

bazz

elemjosh
02-08-2011, 01:06 AM
I also found this topic to be helpful. I made an upload file that has four images. I would like to allow users to "update" one, some, or all of the photos at any time using a MySql UPDATE statement.

When I did this with just one image, I had the variable set up like this:


$encoded = ""; //sets $encoded to blank
$ownerpic = $_FILES['picture']['name'];
if (!empty($ownerpic)) {
copy($picture, "./ownerpics/".$ownerpic."");
$encoded = "ownerpics/".$ownerpic;
}


and the sql statement started this way:



sql="UPDATE profile SET ";
if (!empty($encoded)) {
$sql .= "ownerpic = '$encoded', ";
}
$sql .= "id2 = '$id2', etc, etc


Based on how the multiple upload works, I'm guessing that I can't just set all four pics to blank in the same way, because sometimes users will want to change more than one picture at a time. Any thoughts would be greatly appreciated.

elemjosh
02-10-2011, 02:11 AM
Never mind, I figured it out!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum