PHP form not entering info in database
Posted: Thu Jan 29, 2004 8:13 am
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>