hightechredneck
06-05-2006, 10:37 PM
This script is supposed to upload multiple image files to a mysql db. The image info is written into a table which assigns a new file name. This new file name is supposed to be moved to the main table. The images themselves are uploaded into a file on the server.
What works:
The image is uploaded into the file on the server and the info is written into the table.
What doesn't work:
The new file name isn't written into the main table.
Can anyone help?
Here's the part of the code that is symptomatic:
// Add Image to the database.
if( isset( $_FILES['upload'] )) {
for($i=0;$i<count($_FILES['upload']['name']);$i++) {
$filename = $_FILES['upload']['name'][$i];
$query = "INSERT INTO uploads (file_name, file_size, file_type, upload_date)
VALUES ('{$_FILES['upload']['name'][$i]}' , {$_FILES['upload']['size'][$i]},
'{$_FILES['upload']['type'][$i]}', NOW())";
$result = @mysql_query ($query) or die("Query failed: ".mysql_error()."<br/>".$query);
if ($result) {
//Create the filename.
$extension = explode ('.', $_FILES['upload']['name'][$i]);
$uploadid = mysql_query( "LAST_INSERT_ID()" ); //Upload ID
echo '$filename = $uploadid . '.' . $extension[1]';
//Move file over and update all fields.
if (move_uploaded_file($_FILES['upload']['tmp_name'][$i], "uploads/$filename")){
echo 'Your image(s) have been uploaded.';
for($i = 0, $count = count($_POST['id']); $i < $count; $i++) {
mysql_query('UPDATE ads SET image = "'.$_POST['$filename'][$i].'" WHERE id = "'.$_POST['$id'][$i].'"');
$result = mysql_query($query) or die("Query failed: ".mysql_error()."<br/>".$query);
if ($result) { // If it ran ok.
echo 'Image info has been added to main table.';
}
}
} else {
echo '<p><font color="red">Your image could not be uploaded.</font></p>';
//Remove the record from the database
$query = "DELETE FROM uploads WHERE upload_id = $uploadid";
$result = @mysql_query ($query);
}
} else { // If the query did not run ok.
echo '<p><font color="red">Your submission could not be processed due to a system error.
We apologize for any inconvienence.</font></p>';
}
}
}
} else {
?>
and here's the bulk of the code:
echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">';
$sn = "";
$alt = "";
$id = "";
$filename = "";
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
// This page allows admin to edit ads.
if (isset($_POST['step1'])) { // Handle the form.
$query = "SELECT id, square_number, available FROM ads WHERE available = '1'";
$result = mysql_query ($query);
$i = 0;
while( $row = mysql_fetch_array ($result, MYSQL_NUM )) {
// Validate and select square number
if ( isset( $_POST['existing'] )) {
if ( isset( $_POST['existing'][$i] )) {
$sn = $_POST['existing'][$i];
} else {
$sn = "";
}
} else {
$sn = "";
echo '<p><font color="red">Please select the square number(s)!</font></p>';
}
if ($sn) { // Print info for selected square(s)
$query2 = "SELECT id, square_number, alt, hyper_link, available, name, company, paid, email, height, width, order_id, number_of_squares, image FROM ads WHERE id = " . $sn . " AND available = 1";
$result2 = mysql_query ($query2);
if ($result2) {
echo '
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288">
<fieldset><legend>Edit Me</legend>
<table border="2" width="95%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td><b>ID</b></td>
<td><b>Square Number</b></td>
<td><b>Image Upload 10 pix X 10 pix</b></td>
</tr>';
// Display all the URLs.
while ($row = mysql_fetch_array($result2, MYSQL_NUM)) {
// Display each record.
echo "
<tr>
<td><input type=\"hidden\" name=\"id[]\" value=\"$row[0]\" />$row[0]</td>
<td><input type=\"hidden\" name=\"square_number[]\" value=\"$row[1]\" />$row[1]</td>
<td><input type=\"file\" name=\"upload[]\" value=\"$row[13]\" /></td>
</tr>";
} // end while
} // end-if($result2)
} // end-if($sn)
$i++;
} // end-while
echo '
<tr>
<td align="left" colspan="13"><input type="submit" name="step2" value="Update" /></td>
</tr>
</table>
</fieldset>
</form>'; // Close the table.
} elseif ( isset( $_POST['step2'])) { // end main submit , check for step 2
$query = "SELECT id, square_number, image FROM ads WHERE id = " . $sn . "";
$result = mysql_query ($query);
// Add Image to the database.
if( isset( $_FILES['upload'] )) {
for($i=0;$i<count($_FILES['upload']['name']);$i++) {
$filename = $_FILES['upload']['name'][$i];
$query = "INSERT INTO uploads (file_name, file_size, file_type, upload_date)
VALUES ('{$_FILES['upload']['name'][$i]}' , {$_FILES['upload']['size'][$i]},
'{$_FILES['upload']['type'][$i]}', NOW())";
$result = @mysql_query ($query) or die("Query failed: ".mysql_error()."<br/>".$query);
if ($result) {
//Create the filename.
$extension = explode ('.', $_FILES['upload']['name'][$i]);
$uploadid = mysql_query( "LAST_INSERT_ID()" ); //Upload ID
echo '$filename = $uploadid . '.' . $extension[1]';
//Move file over and update all fields.
if (move_uploaded_file($_FILES['upload']['tmp_name'][$i], "uploads/$filename")){
echo 'Your image(s) have been uploaded.';
for($i = 0, $count = count($_POST['id']); $i < $count; $i++) {
mysql_query('UPDATE ads SET image = "'.$_POST['$filename'][$i].'" WHERE id = "'.$_POST['$id'][$i].'"');
$result = mysql_query($query) or die("Query failed: ".mysql_error()."<br/>".$query);
if ($result) { // If it ran ok.
echo 'Image info has been added to main table.';
}
}
} else {
echo '<p><font color="red">Your image could not be uploaded.</font></p>';
//Remove the record from the database
$query = "DELETE FROM uploads WHERE upload_id = $uploadid";
$result = @mysql_query ($query);
}
} else { // If the query did not run ok.
echo '<p><font color="red">Your submission could not be processed due to a system error.
We apologize for any inconvienence.</font></p>';
}
}
}
} else {
?>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288">
<fieldset><legend></legend>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="5"><font size="3" color="#0000CC" face="Arial, Helvetica, sans-serif"><b>Enter
your advertisers info here:</b></font></td>
<td colspan="2" rowspan="2"> </td>
</tr>
<tr>
<td colspan="5" align="center">
<p> </p>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>Step 1:</b></font></p>
</td>
</tr>
<tr>
<td colspan="2" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><b>Square
Numbers: Select the square numbers that your client purchased. <font color="#FF0000">(eg:
1_1, 1_2, 1_3)</font></b></font></td>
<td width="12%" valign="top"> </td>
<td colspan="2" valign="top"> <font size="2" face="Arial, Helvetica, sans-serif">
<select name="existing[]" multiple size="12">
<option selected>Make Selections Here</option>
<?php // Retrieve all square numbers and add to the pull down menu.
$query = "SELECT id, square_number from ads";
$result = @mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['id']}\">{$row['square_number']}</option>\n";
}
?>
</select>
</font></td>
<td colspan="2" valign="top"> </td>
</tr>
<tr>
<td valign="top" colspan="2"> </td>
<td valign="top" width="12%"> </td>
<td width="18%" valign="top"> </td>
<td width="22%" valign="top"> </td>
</tr>
<tr>
<td valign="top" colspan="2"> </td>
</tr>
<tr align="center">
<td colspan="7"><font face="Arial, Helvetica, sans-serif" size="2"><b><i>Click"Submit",
to submit your advertisers info!<br>
</i></b></font></td>
</tr>
<tr>
<td align="center" colspan="7"> <font face="Arial, Helvetica, sans-serif">
<input type="submit" name="step1" value="Submit" />
</font></td>
</tr>
</table>
</fieldset>
</form><!-- End of Form -->
Thanks
T
What works:
The image is uploaded into the file on the server and the info is written into the table.
What doesn't work:
The new file name isn't written into the main table.
Can anyone help?
Here's the part of the code that is symptomatic:
// Add Image to the database.
if( isset( $_FILES['upload'] )) {
for($i=0;$i<count($_FILES['upload']['name']);$i++) {
$filename = $_FILES['upload']['name'][$i];
$query = "INSERT INTO uploads (file_name, file_size, file_type, upload_date)
VALUES ('{$_FILES['upload']['name'][$i]}' , {$_FILES['upload']['size'][$i]},
'{$_FILES['upload']['type'][$i]}', NOW())";
$result = @mysql_query ($query) or die("Query failed: ".mysql_error()."<br/>".$query);
if ($result) {
//Create the filename.
$extension = explode ('.', $_FILES['upload']['name'][$i]);
$uploadid = mysql_query( "LAST_INSERT_ID()" ); //Upload ID
echo '$filename = $uploadid . '.' . $extension[1]';
//Move file over and update all fields.
if (move_uploaded_file($_FILES['upload']['tmp_name'][$i], "uploads/$filename")){
echo 'Your image(s) have been uploaded.';
for($i = 0, $count = count($_POST['id']); $i < $count; $i++) {
mysql_query('UPDATE ads SET image = "'.$_POST['$filename'][$i].'" WHERE id = "'.$_POST['$id'][$i].'"');
$result = mysql_query($query) or die("Query failed: ".mysql_error()."<br/>".$query);
if ($result) { // If it ran ok.
echo 'Image info has been added to main table.';
}
}
} else {
echo '<p><font color="red">Your image could not be uploaded.</font></p>';
//Remove the record from the database
$query = "DELETE FROM uploads WHERE upload_id = $uploadid";
$result = @mysql_query ($query);
}
} else { // If the query did not run ok.
echo '<p><font color="red">Your submission could not be processed due to a system error.
We apologize for any inconvienence.</font></p>';
}
}
}
} else {
?>
and here's the bulk of the code:
echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">';
$sn = "";
$alt = "";
$id = "";
$filename = "";
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
// This page allows admin to edit ads.
if (isset($_POST['step1'])) { // Handle the form.
$query = "SELECT id, square_number, available FROM ads WHERE available = '1'";
$result = mysql_query ($query);
$i = 0;
while( $row = mysql_fetch_array ($result, MYSQL_NUM )) {
// Validate and select square number
if ( isset( $_POST['existing'] )) {
if ( isset( $_POST['existing'][$i] )) {
$sn = $_POST['existing'][$i];
} else {
$sn = "";
}
} else {
$sn = "";
echo '<p><font color="red">Please select the square number(s)!</font></p>';
}
if ($sn) { // Print info for selected square(s)
$query2 = "SELECT id, square_number, alt, hyper_link, available, name, company, paid, email, height, width, order_id, number_of_squares, image FROM ads WHERE id = " . $sn . " AND available = 1";
$result2 = mysql_query ($query2);
if ($result2) {
echo '
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288">
<fieldset><legend>Edit Me</legend>
<table border="2" width="95%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td><b>ID</b></td>
<td><b>Square Number</b></td>
<td><b>Image Upload 10 pix X 10 pix</b></td>
</tr>';
// Display all the URLs.
while ($row = mysql_fetch_array($result2, MYSQL_NUM)) {
// Display each record.
echo "
<tr>
<td><input type=\"hidden\" name=\"id[]\" value=\"$row[0]\" />$row[0]</td>
<td><input type=\"hidden\" name=\"square_number[]\" value=\"$row[1]\" />$row[1]</td>
<td><input type=\"file\" name=\"upload[]\" value=\"$row[13]\" /></td>
</tr>";
} // end while
} // end-if($result2)
} // end-if($sn)
$i++;
} // end-while
echo '
<tr>
<td align="left" colspan="13"><input type="submit" name="step2" value="Update" /></td>
</tr>
</table>
</fieldset>
</form>'; // Close the table.
} elseif ( isset( $_POST['step2'])) { // end main submit , check for step 2
$query = "SELECT id, square_number, image FROM ads WHERE id = " . $sn . "";
$result = mysql_query ($query);
// Add Image to the database.
if( isset( $_FILES['upload'] )) {
for($i=0;$i<count($_FILES['upload']['name']);$i++) {
$filename = $_FILES['upload']['name'][$i];
$query = "INSERT INTO uploads (file_name, file_size, file_type, upload_date)
VALUES ('{$_FILES['upload']['name'][$i]}' , {$_FILES['upload']['size'][$i]},
'{$_FILES['upload']['type'][$i]}', NOW())";
$result = @mysql_query ($query) or die("Query failed: ".mysql_error()."<br/>".$query);
if ($result) {
//Create the filename.
$extension = explode ('.', $_FILES['upload']['name'][$i]);
$uploadid = mysql_query( "LAST_INSERT_ID()" ); //Upload ID
echo '$filename = $uploadid . '.' . $extension[1]';
//Move file over and update all fields.
if (move_uploaded_file($_FILES['upload']['tmp_name'][$i], "uploads/$filename")){
echo 'Your image(s) have been uploaded.';
for($i = 0, $count = count($_POST['id']); $i < $count; $i++) {
mysql_query('UPDATE ads SET image = "'.$_POST['$filename'][$i].'" WHERE id = "'.$_POST['$id'][$i].'"');
$result = mysql_query($query) or die("Query failed: ".mysql_error()."<br/>".$query);
if ($result) { // If it ran ok.
echo 'Image info has been added to main table.';
}
}
} else {
echo '<p><font color="red">Your image could not be uploaded.</font></p>';
//Remove the record from the database
$query = "DELETE FROM uploads WHERE upload_id = $uploadid";
$result = @mysql_query ($query);
}
} else { // If the query did not run ok.
echo '<p><font color="red">Your submission could not be processed due to a system error.
We apologize for any inconvienence.</font></p>';
}
}
}
} else {
?>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288">
<fieldset><legend></legend>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="5"><font size="3" color="#0000CC" face="Arial, Helvetica, sans-serif"><b>Enter
your advertisers info here:</b></font></td>
<td colspan="2" rowspan="2"> </td>
</tr>
<tr>
<td colspan="5" align="center">
<p> </p>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>Step 1:</b></font></p>
</td>
</tr>
<tr>
<td colspan="2" valign="top"><font face="Arial, Helvetica, sans-serif" size="2"><b>Square
Numbers: Select the square numbers that your client purchased. <font color="#FF0000">(eg:
1_1, 1_2, 1_3)</font></b></font></td>
<td width="12%" valign="top"> </td>
<td colspan="2" valign="top"> <font size="2" face="Arial, Helvetica, sans-serif">
<select name="existing[]" multiple size="12">
<option selected>Make Selections Here</option>
<?php // Retrieve all square numbers and add to the pull down menu.
$query = "SELECT id, square_number from ads";
$result = @mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['id']}\">{$row['square_number']}</option>\n";
}
?>
</select>
</font></td>
<td colspan="2" valign="top"> </td>
</tr>
<tr>
<td valign="top" colspan="2"> </td>
<td valign="top" width="12%"> </td>
<td width="18%" valign="top"> </td>
<td width="22%" valign="top"> </td>
</tr>
<tr>
<td valign="top" colspan="2"> </td>
</tr>
<tr align="center">
<td colspan="7"><font face="Arial, Helvetica, sans-serif" size="2"><b><i>Click"Submit",
to submit your advertisers info!<br>
</i></b></font></td>
</tr>
<tr>
<td align="center" colspan="7"> <font face="Arial, Helvetica, sans-serif">
<input type="submit" name="step1" value="Submit" />
</font></td>
</tr>
</table>
</fieldset>
</form><!-- End of Form -->
Thanks
T