I am using the following script to upload up to 6 images at once and insert them into a database. All of the images get uploaded fine, but only the first image gets inserted into the database. Can somebody please take a look at my script and tell me why this is?
$numoffile = 6;
$uploaddir = '../'.$cat.'Listings/images/';
if ($form == "upload") {
for ($i=0; $i<$numoffile; $i++) {
if (trim($_FILES['images']['name'][$i])!="") {
$newfile = $uploaddir.$_FILES['images']['name'][$i];
$fileName = basename($_FILES['images']['name'][$i]);
echo $_FILES['images']['name'][$i];
echo "<br/>";
if(move_uploaded_file($_FILES['images']['tmp_name'][$i], $newfile)){
$sql = "INSERT INTO photos (`id`, `listType`, `fileName`)
VALUES ('$listID', '$cat', '$fileName');" or die ('Could not insert into database because: ' . mysql_error());
$result = mysql_query($sql);
}
$j++;
}
}
}
if(isset($j)&&$j>0){
echo "J: $j<br/>";
echo "<br/><strong>The above photo(s) were uploaded successfully.</strong><br/><br/>";
}
echo"
<form action='$path/admin/uploadPhotos.php?cat=".$cat."' method='post' name='upload' enctype='multipart/form-data'>
<input type='hidden' name='MAX_FILE_SIZE' value='307200' />
<table cellpadding='3' cellspacing='0' border='0' width='100%' summary='This table is for display only.'>
<tr>
<td><strong>Select a listing below that you wish to upload photos for:</strong><br/>
<select name='listID' id='textBoxStyle'>";
$sql = "Select id, address, city, state From ".$cat." Order By id ASC";
$result = mysql_query($sql);
if($result){
while($listing = mysql_fetch_array($result)){
$listDetail= "".$listing['id'].": ".$listing['address']." ".$listing['city']."";
echo "<option value='".$listing['id']."'>".$listDetail."</option>";
}
}
echo"
</select><br/><br/>
</td>
</tr>
<tr>
<td><strong>Select up to six(6) images to upload:</strong><br/>
*Note: Maximum file upload size is 300kb total for all images combined.</td>
</tr>";
for($i=0; $i<$numoffile; $i++) {
echo"
<tr>
<td><input type='file' name='images[]' id='textBoxStyle'></td>
</tr>";
}
echo"
<tr>
<td><input type='submit' name='upload' value='Upload' class='submitStyle'/>
<input type='hidden' name='form' value='upload'></td>
</tr>
</table>
</form>
The insert query is in the same loop to upload the images one by one, so why doesn't it query the database once for each image being uploaded as well?
Thanks in advance.