I can't figure out what the problem is with my code. What I want it to do is add a new sandwich to the database when all fields are filed out and an image is uploaded. If product name field is not entered, the sandwich selected in the dropdown box should be updated to a new (from whatever the user entered) description, image, origin, etc.
1. When I try to add a new sandwich, it always echoes "All fields not filled out" but the sandwich is added to the DB with all fields except the image name.
2. When I try to use the dropdown box and not enter a product name, it goes through the same thing above and never updates the DB. It's like it never goes to the else statement.
<?php
echo '<html>
<head>
<title>Uploading...</title>
</head>
<body>
<h3>Uploading file...</h3>';
if ($_FILES['filename']['error'] > 0)
{
echo 'Problem: ';
switch ($_FILES['filename']['error'])
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}
// put the file where we'd like it
$upfile = './images/'.$_FILES['filename']['name'];
if (is_uploaded_file($_FILES['filename']['tmp_name']))
{
if (!move_uploaded_file($_FILES['filename']['tmp_name'], $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: ';
echo $_FILES['filename']['name'];
exit;
}
echo '<p>File uploaded successfully<br><br>';
if (isset($_POST['description'])) {
$description = $_POST['description'];
} else {
$description = $_FILES['filename']['name'];
}
// show what was uploaded
echo '<p>Preview of uploaded file:<br><hr>';
echo "<a href='$upfile'>$description</a>";
echo '<br><hr>';
echo '</body></html>';
// Set up the database connection.
require_once ('mysqli_connect.php');
//if name selected from drop down box
if (isset($_POST['product_name']))
{
if (isset($_POST['product_name']) && isset($_POST['product_description']) && isset($_POST['price']))
{
// add new sandwich
$product_name = $_POST['product_name'];
$product_description = $_POST['product_description'];
$price = $_POST['price'];
$query = "INSERT INTO products (product_name, product_description, price)
VALUES ('$product_name', '$product_description', '$price')";
$result = mysqli_query ($dbc, $query);
$productid = mysqli_insert_id($dbc);
if (isset($_POST['origin']))
{
$origin = $_POST['origin'];
$query = "INSERT INTO origins (origin, productid) VALUES ('$origin', '$productid')";
$result = mysqli_query ($dbc, $query);
}
if (isset($_POST['filename']))
{
$imagename = $_POST['filename'];
$query = "INSERT INTO images (productid, image_name) VALUES ('$productid', '$imagename')";
$result = mysqli_query ($dbc, $query);
}
else
{
echo '<p>Error - All fields are required!</p>';
}
}
else
{
// update product_name selected
if (isset($_POST['product_id']) && isset($_POST['product_description']) && isset($_POST['price']))
{
$product_id = $_POST[product_id];
$product_description = $_POST['product_description'];
$price = $_POST['price'];
$query = "UPDATE products SET product_description='$product_description', price='$price',
WHERE product_id='$product_id'";
$result = mysqli_query ($dbc, $query);
if (isset($_POST['origin']))
{
$origin = $_POST['origin'];
$query = "INSERT INTO origins (origin, productid) VALUES ('$origin', '$productid')";
$result = mysqli_query ($dbc, $query);
}
if (isset($_POST['filename']))
{
$imagename = $_POST['filename'];
$query = "UPDATE images SET image_name='$imagename' WHERE product_id='$product_id'";
$result = mysqli_query ($dbc, $query);
}
}
else
{
echo '<p>Error!</p>';
}
}
}
echo 'Productid: ' . $productid . ' ' . ' Product Name: ' . $product_name . ' ' . ' Product Description: ' . $product_description . ' ' . ' Image Name: ' .
$imagename . ' Origin: ' . $origin . ' Price: ' . $price . '<br>';
if ($result) { // If it ran OK.
echo '<p>Data has been entered successfully.</p>';
} else { // If it did not run OK.
echo '<p>Data has not been processed due to a system error.</p>';
}
?>
dropdown menu
<html>
<body>
<h3>Homework 2</h3>
<form enctype="multipart/form-data" action="process_menu2.php" method="post">
<p><table border=0>
<TR>
<TD>Sandwich Name: </TD>
<TD>
<?php
require_once ('mysqli_connect.php');
$sql = "Select products.productid, product_name
from products, origins, images, prod_origins
Where products.productid = prod_origins.productid
And products.productid = images.productid
And PROD_ORIGINS.ORIGINID = ORIGINS.ORIGINID ORDER BY product_name ASC";
$result = mysqli_query($dbc, $sql) or die( "Could not execute query: $query" );
$str = "<SELECT NAME=product_id><BR>\n";
while ($row = mysqli_fetch_array($result)) {
$str .= '<OPTION VALUE=' . $row['product_id'] . '>' . $row['product_name'] . '<BR>' . "\n";
}
$str .= '</SELECT>';
echo $str
?>
</TD>
</TR>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<TR>
<TD>Enter a Sandwich Name:</TD>
<TD><input type="text" name="product_name" size="41"></TD>
</TR>
<TR>
<TR>
<TD>Sandwich Description:</TD>
<TD><input type="text" name="product_description" size="41"></TD>
</TR>
<TR>
<TD>Sandwich Origin:</TD>
<TD><input type="text" name="origin" size="41"></TD>
</TR>
<TR>
<TD>Sandwich Price:</TD>
<TD><input type="text" name="price" size="41"></TD>
</TR>
<TR>
<TR>
<TR>
<TD>Upload sandwich image: </TD>
<TD><input type="file" name="filename" size="41"></TD>
</TR>
</table>
<BR><BR>
<input type="submit" value="Submit">
</form>
</body>
</html>