1 Replies - 1079 Views - Last Post: 04 May 2010 - 07:56 AM Rate Topic: -----

#1 gymangel812  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 15-February 10

Uploading images, Setting variable as drop down item, Checking field f

Posted 03 May 2010 - 06:17 PM

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>



Is This A Good Question/Topic? 0
  • +

Replies To: Uploading images, Setting variable as drop down item, Checking field f

#2 gymangel812  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 15-February 10

Re: Uploading images, Setting variable as drop down item, Checking field f

Posted 04 May 2010 - 07:56 AM

i think i got the not empty part but i'm still stuck on 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?

<?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
	    if (!empty(product_description))
	    {
	    $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 price box not empty
		if (!empty(price))
		{
		$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
	   if (!empty(origin))
	   {
	    $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
    	if (!empty(filename))
		{
	    $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>';

        }

?>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1