Hi all, My goal is to upload a single image and rename it base on the product_id already stored in a products table. My challenge comes from not being able to pull $pid (product_id) using both $POST and $GET in the form area as well as the query. I will post the code and if you can tell me how to set $pid=product_id that would be fantastic as right now my query is incomplete because I do not have a value for $pid.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Add an Image</title>
</head>
<body>
<?php # upload_test6.php
// This page allows the administrator to add a print (product).
include ('/header.php');
require_once ('./mysqli_connect.php');
if (isset($_POST['submitted'])) {
$pid = $_POST['pid'];
echo "My PID: $pid";// No value for $pid.
// Check for an image:
if (is_uploaded_file ($_FILES['image']['tmp_name'])) {
// Create a temporary file name:
$temp = './uploads/' . md5($_FILES['image']['name']);
// Move the file over:
if (move_uploaded_file($_FILES['image']['tmp_name'], $temp)) {
echo '<p>The file has been uploaded!</p>';
// Set the $i variable to the image's name:
$i = $_FILES['image']['name'];
} else { // Couldn't move the file over.
$errors[] = 'The file could not be moved.';
$temp = $_FILES['image']['tmp_name'];
}
} else { // No uploaded file.
$errors[] = 'No file was uploaded.';
$temp = NULL;
}
$q = "SELECT product_id FROM products WHERE product_id={$_POST['pid']}";
echo "<pre>"; echo '$q'; print_r("$q"); echo "</pre>";
$r = mysqli_query($dbc,$q)
or die("Error: ".mysqli_error($dbc));
$row = mysqli_fetch_array ($r);
rename ($temp, "./uploads/$pid"); // Rename the image:
// Delete the medium uploaded file if it still exists:
if ( isset($temp) && file_exists ($temp) && is_file($temp) ) {
unlink ($temp);
}
// Check for any errors and print them:
if ( !empty($errors) && is_array($errors) ) {
echo '<h1>Error!</h1>
<p style="font-weight: bold; color: #C00">The following error(s) occurred:<br />';
foreach ($errors as $msg) {
echo " - $msg<br />\n";
}
echo 'Please reselect the Product image and try again.</p>';
}
} //End of submitted if statement
mysqli_close($dbc);
// Display the form...
?>
<div id="middle_column">
<h1>Add Image</h1>
<form enctype="multipart/form-data" action="upload_test6.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<fieldset><legend>Fill out the form to add a print to the catalog:</legend>
<p><b>Image:</b> <input type="file" name="image" /></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div> <!-- Ending add_b div -->
<input type="hidden" name="pid" value="<?php echo $_GET['pid']; ?>" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
Thanks in advance,