I want the program to update or add to a database. If an item is selected from the dropdown menu (filled with the product names already in the DB), they I need to update the fields with the information typed by the user or update the picture that is uploaded. If a drop down box is not selected, I need to add a new sandwich to the DB.


1. The images aren't going into the folder. Is this possibly due to Vista having problems with not being able to copy things into Program files?

2. How do I assign a variable to whatever the string was selected in the dropdown menu?

3. How do I check if the input boxes, (price, origin, description) are empty?


I basically have what i want the program to do in english (noted with ***):

process_menu_hw2.php

<?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']['product_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']['prduct_name'];
    	exit;
}

echo '<p>File uploaded successfully<br><br>';

if (isset($_POST['product_description'])) {
	$description = $_POST['description'];
} else {
        $description = $_FILES['filename']['product_name'];
}

// show what was uploaded
echo '<p>Preview of uploaded file:<br><hr>';
echo "<a href='$upfile'>$product_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
    // update product_name selected

        $product_name = *** selected name
        $product_id = *** hidden product ID from selected box

	    *** if description box not empty
	    $product_description = $_POST['product_description'];
	    $query = "UPDATE products SET product_description='$product_description' WHERE product_name='$product_name"';

	    $result = mysqli_query ($dbc, $query);
	    echo '<p>Description updated</p>';

		*** if description box not empty
		$price = $_POST['price'];
		$query = "UPDATE products SET price='$price' WHERE product_name='$product_name"';

		$result = mysqli_query ($dbc, $query);
	    echo '<p>Price updated</p>';

	    *** if origin box not empty

	    $origin = $_POST['origin'];

	    $query = "INSERT INTO origins (origin, productid) VALUES ('$origin', '$productid')";

    	$result = mysqli_query ($dbc, $query);
    	echo '<p>Origin updated</p>';

    	*** if filename box is not empty

	    $imagename = $_POST['image_name'];

	    $query = "UPDATE images SET image_name='$imagename' WHERE product_id='$product_id'";

    	$result = mysqli_query ($dbc, $query);
    	echo '<p>Image name updated</p>';


***else
	// 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);

    $origin = $_POST['origin'];

    $query = "INSERT INTO origins (origin, productid) VALUES ('$origin', '$productid')";

    $result = mysqli_query ($dbc, $query);

    $imagename = $_POST['image_name'];

    $query = "INSERT INTO images (productid, image_name) VALUES ('$productid', '$imagename')";

    $result = mysqli_query ($dbc, $query);


echo 'Productid: ' . $productid . ' ' . ' Product Name: ' . $productname . ' ' . ' 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_menuhw2.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, product_description, origin, image_name, price
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_name><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>

dont have time to read through all the code, but what you want sounds pretty simple.

firstly to address something else :
"The images aren't going into the folder. Is this possibly due to Vista having problems with not being able to copy things into Program files?"
--> use absolute paths. btw do you have a testing server installed? Like wamp or xampp? or maybe ISS?

"How do I assign a variable to whatever the string was selected in the dropdown menu?"
--> use the count method;
something like $getcount = count($_POST); and

for ($i = 1; $i <= $getcount; $i++) {
    echo $i;
}

and submit your menu values as <option value = "xxx" name = "xxx"[]></option>

good luck

oops, forgot.

your field name would be name=xxx[$i]

thanks but i'm still a bit confused. the drop down menu has 2 items in it (a hidden product id and the product name). how would i get each of those items and assign each one to a variable?

hey

sorry for the delay.

got loads of work here

I will take all your code and try to work something out for you, however I cannot promise a definite solution since I am not clear on what your request is.

i'LL TRY TO DO IT ASAP

hey

sorry but I cant do anything, because the code is incomplete, (I dont have the database structure and connect file)

and I dont completely understand your request.

however, for this : "How do I check if the input boxes, (price, origin, description) are empty?"

use something like <?php if (!isset($_POST)) { echo 'that box or value is empty!!!'; } ?>

hey

sorry but I cant do anything, because the code is incomplete, (I dont have the database structure and connect file)

and I dont completely understand your request.

however, for this : "How do I check if the input boxes, (price, origin, description) are empty?"

use something like <?php if (!isset($_POST)) { echo 'that box or value is empty!!!'; } ?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.