Page 1 of 1

PHP form not entering info in database

Posted: Thu Jan 29, 2004 8:13 am
by Trevor_needs_help
i am fairly new to php, using a book i have written a form to submit data into a mysql database the problem is that when submit is hit the form loops to the error statement that has been put in "Your submission could not be processed due to a system error" but the file uploads correctly. please can someone help me i have been trying for around a week to fix this it is driving me made.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/2000/REC-xhtml1-20000126/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 a meal</title>
</head>
<body>
<?php 
// This page allows the administrator to add a meal.

require_once ('../../mysql_connect.php'); // Connect to the database.

if (isset($_POST['submit'])) { // Handle the form.
	
	// Validate the meal_name, image, size, price, and description.

	// Check for a meal name.
	if (!empty($_POST['meal_name'])) {
		$pn = escape_data($_POST['meal_name']);
	} else {
		$pn = FALSE;
		echo '<p><font color="red">Please enter the meals name!</font></p>';
	}
	
	// Check for an image (not required).
	if (is_uploaded_file ($_FILES['image']['tmp_name'])) {
		if (move_uploaded_file($_FILES['image']['tmp_name'], "../../uploads/{$_FILES['image']['name']}")) { // Move the file over.

			echo '<p>The file has been uploaded!</p>';

		} else { // Couldn't move the file over.
			echo '<p><font color="red">The file could not be moved.</font></p>';
			$i = '';
		}
		$i = $_FILES['image']['name'];
	} else {
		$i = FALSE;
		echo'<p><font color="red">Please Upload the meals image file</font></p>';
	}
	
	//Check for a size (not required).
	if (!empty($_POST['size'])) {
		$s = escape_data($_POST['size']);
	} else {
		$s = FALSE;
		echo '<p><font color="red">Please enter the size of the meal</font></p>';
	}
	
	// Check for a price.
	if (is_numeric($_POST['price'])) {
		$p = $_POST['price'];
	} else {
		$p = FALSE;
		echo '<p><font color="red">Please enter the meals price!</font></p>';
	}
	
	// Check for a description 
	if (!empty($_POST['descripiton'])) {
		$d = escape_data($_POST['descripiton']);
	} else {
		$d = FALSE;
 	echo '<p><font color="red">Please enter the meals description</font></p>';
	}
	
	if ($pn &&  $i && $s && $p && $d) {
	
		// Add the meal to the database.
		$query = "INSERT INTO meal (meal_name,image_name, size,  price, ) VALUES ($pn, '$p',$s, '$i', $d)";
	if ($result = @mysql_query ($query)) { // Worked.
		echo '<p>The meal has been added.</p>';
	} else { // If the query did not run OK.
			echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>';
		}
		
	} else { // Failed a test.
			echo '<p><font color="red">Please click "back" and try again.</font></p>';
	}
	
} else {// Display the form.
?>
	
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="524288">

<fieldset><legend>Fill out the form to add a meal to the catalog:</legend>

<p><b>meal Name:</b> <input type="text" name="meal_name" size="30" maxlength="60" /></p>

<p><b>Image:</b> <input type="file" name="image" /></p>

<p><b>Size:</b> <input type="text" name="size" size="30" maxlength="60" /></p>

<p><b>Price:</b> <input type="text" name="price" size="10" maxlength="10" /><br /><small>Do not include the dollar sign or commas.</small></p>

<p><b>Description:</b> <textarea name="descripiton" cols="40" rows="5"></textarea></p>

</fieldset>
	
<div align="center"><input type="submit" name="submit" value="Submit" /></div>

</form><!-- End of Form -->
<?php
}
mysql_close();
?>
</body>
</html>

Posted: Thu Jan 29, 2004 11:23 am
by infolock
try replacing this :

Code: Select all

$query = "INSERT INTO meal (meal_name,image_name, size,  price, ) VALUES ($pn, '$p',$s, '$i', $d)"; 
   if ($result = @mysql_query ($query)) { // Worked. 
      echo '<p>The meal has been added.</p>'; 
   } else { // If the query did not run OK. 
         echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>'; 
      }

with this :

Code: Select all

$query = "INSERT INTO meal (meal_name,image_name, size,  price, ) VALUES ('".$pn."', '".$p."', '".$s."', '".$i."', '".$d."')"; 
$result = mysql_query($query) or die(MySQL_Error());
echo '<p>The meal has been added.</p>';
and see if you get any errors. if you do, post them and we'll go from there. overall, ur code looks like it needs to be re-written. you are doing a lot of unecessary if / else's...

Posted: Thu Jan 29, 2004 12:37 pm
by m3rajk
before trying infolock, i'd change

Code: Select all

$query = "INSERT INTO meal (meal_name,image_name, size,  price, ) VALUES ($pn, '$p',$s, '$i', $d)";
   if ($result = @mysql_query ($query)) { // Worked.
      echo '<p>The meal has been added.</p>';
   } else { // If the query did not run OK.
         echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>';
      }
to

Code: Select all

$query = "INSERT INTO meal (meal_name,image_name, size,  price, ) VALUES ($pn, '$p',$s, '$i', $d)";
   if ($result = @mysql_query ($query)) { // Worked.
      echo '<p>The meal has been added.</p>';
   } else { // If the query did not run OK.
         $debug='error--'.mysql_errno($db).': ".mysql_error($db);
         echo '<p><font color="red">Your submission could not be processed due to a system error.</font><br />$debug</p>';
      }
and see if you can't figure out what's wrong then
at the very least give us that information

THANKS - SHOPPING CARTS

Posted: Sat Jan 31, 2004 9:28 am
by Trevor_needs_help
I would like to say thank you for posting replys to my question the code from INFOLOCK fixed the problem. i work fine. just wanted to ask wether you had any advise on creating a "shopping cart" for the page i am under taking