adamjw3
asked on
database insert script php
i have this script which i'm goin to use for a simple ecommerce shop admin section but i can't seem to get it to add the products which is a major flaw for any ecommerce site!
here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add a Product</title>
</head>
<body>
<?php
//This script will enable the admin staff to add a prodcut to the database
//include the database connection
require_once('mysql_connec t.php');
//see if form has been submitted
if (isset($_POST['submitted'] )){
//form validation
if(!empty($_POST['product_ name'])){
$pn = escape_data($_POST['prodcu t_name']);
} else {
$pn = FALSE;
echo '<p style="color:red">Please enter a prodcut name</p>';
}
//check image
if (is_uploaded_file ($_FILES['image'] ['tmp_name'])){
if (move_uploaded_file($_FILE S['image'] ['tmp_name '], "{$_FILES['image']['name'] }")){
echo '<p> The file has been uploaded </p>';
} else {
echo '<p style="color:red">file was not uploaded</p>';
$i = FALSE;
}
$i = $_FILES['image']['name'];
} else {
$i = FALSE;
}
//Validate the rest of the fields
if(!empty($_POST['size'])) {
$s = escape_data($_POST['size'] );
} else {
$s = FALSE;
echo '<p style="color:red">Please enter a size</p>';
}
if(!empty($_POST['descript ion'])){
$d = escape_data($_POST['descri ption']);
} else {
$d = FALSE;
echo '<p style="color:red">Please enter a description</p>';
}
//Check price
if(is_numeric($_POST['pric e'])){
$p = (float) $_POST['price'];
} else {
$p = FALSE;
echo '<p style="color:red">Please enter a price</p>';
}
//The product must now be assigned a menu item to go under
if ($_POST['menu1'] == 'new'){
//if its a new one then add it to the database motherfuckers!!!
$query = 'INSERT INTO menu (menu_name) Values (';
if (!empty($_POST['menu_name' ])){
$query .= "'" . escape_data($_POST['menu_n ame']) . "')";
$result = mysql_query($query);
$a = mysql_insert_id();
} else {
$a = FALSE;
echo '<p style="color:red">Please enter the menu name</p>';
}
} else if ( ($_POST['menu'] =='existing') && ($_POST['existing'] > 0)) {
$a = (int) $_POST['existing'];
} else {
$a = FALSE;
echo '<p style="color:red;">Please enter or select a menu item for this product</p>';
}
if ($pn && $i && $s && $d && $a) {//check eveything is ok up to here
//add product to database.
$query = "INSERT INTO products (product_id, product_name, price, size, description, image_name) VALUES ( $a, '$pn', '$p', '$s', '$d', '$i')";
if ($result = mysql_query($dbc, $query)){
echo '<p>product has been added</p>';
} else {
echo '<p> could not do it</p>';
}
} else {
echo '<p>click back and try again</p>';
}
} else { //display the form
?>
<form action="add_product.php" method="post" enctype="multipart/form-da ta">
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<fieldset>
<legend>Fill out the form dude!</legend>
<p>Product name:<input name="product_name" type="text" size="30" maxlength="60" /></p>
<p>image:<input name="image" type="file" /></p>
<br />
<br />
<h3>Menu Item</h3>
<p><input name="menu" type="radio" value="existing" /> Existing =>
<select name="existing">
<option>select one</option>
<? php // get the current list of menu items and display them in the select box geezers!
$query = "SELECT menu_name, menu_id FROM menu ORDER BY menu_name ASC";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result , MYSQLI_ASSOC)){
echo "<option value=\"{$row['menu_id']}\ ">{$row['m enu_name'] </option>\ n";
}
mysqli_close($dbc);//close database conn
?>
</select></p>
<input name="menu1" type="radio" value="new" /> new =>
<p>New menu :<input name="menu_name" type="text" /></p>
<p>Price:<input name="price" type="text" /></p>
<p>size:<input name="size" type="text" /></p>
<p>description:<textarea name="description" cols="40" rows="5" /></textarea></p>
</fieldset>
<input name="submit" type="submit" value="submit" />
<input name="submitted" type="hidden" value="TRUE" />
</form>
<?php
}
?>
</body>
</html>
here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add a Product</title>
</head>
<body>
<?php
//This script will enable the admin staff to add a prodcut to the database
//include the database connection
require_once('mysql_connec
//see if form has been submitted
if (isset($_POST['submitted']
//form validation
if(!empty($_POST['product_
$pn = escape_data($_POST['prodcu
} else {
$pn = FALSE;
echo '<p style="color:red">Please enter a prodcut name</p>';
}
//check image
if (is_uploaded_file ($_FILES['image'] ['tmp_name'])){
if (move_uploaded_file($_FILE
echo '<p> The file has been uploaded </p>';
} else {
echo '<p style="color:red">file was not uploaded</p>';
$i = FALSE;
}
$i = $_FILES['image']['name'];
} else {
$i = FALSE;
}
//Validate the rest of the fields
if(!empty($_POST['size']))
$s = escape_data($_POST['size']
} else {
$s = FALSE;
echo '<p style="color:red">Please enter a size</p>';
}
if(!empty($_POST['descript
$d = escape_data($_POST['descri
} else {
$d = FALSE;
echo '<p style="color:red">Please enter a description</p>';
}
//Check price
if(is_numeric($_POST['pric
$p = (float) $_POST['price'];
} else {
$p = FALSE;
echo '<p style="color:red">Please enter a price</p>';
}
//The product must now be assigned a menu item to go under
if ($_POST['menu1'] == 'new'){
//if its a new one then add it to the database motherfuckers!!!
$query = 'INSERT INTO menu (menu_name) Values (';
if (!empty($_POST['menu_name'
$query .= "'" . escape_data($_POST['menu_n
$result = mysql_query($query);
$a = mysql_insert_id();
} else {
$a = FALSE;
echo '<p style="color:red">Please enter the menu name</p>';
}
} else if ( ($_POST['menu'] =='existing') && ($_POST['existing'] > 0)) {
$a = (int) $_POST['existing'];
} else {
$a = FALSE;
echo '<p style="color:red;">Please enter or select a menu item for this product</p>';
}
if ($pn && $i && $s && $d && $a) {//check eveything is ok up to here
//add product to database.
$query = "INSERT INTO products (product_id, product_name, price, size, description, image_name) VALUES ( $a, '$pn', '$p', '$s', '$d', '$i')";
if ($result = mysql_query($dbc, $query)){
echo '<p>product has been added</p>';
} else {
echo '<p> could not do it</p>';
}
} else {
echo '<p>click back and try again</p>';
}
} else { //display the form
?>
<form action="add_product.php" method="post" enctype="multipart/form-da
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<fieldset>
<legend>Fill out the form dude!</legend>
<p>Product name:<input name="product_name" type="text" size="30" maxlength="60" /></p>
<p>image:<input name="image" type="file" /></p>
<br />
<br />
<h3>Menu Item</h3>
<p><input name="menu" type="radio" value="existing" /> Existing =>
<select name="existing">
<option>select one</option>
<? php // get the current list of menu items and display them in the select box geezers!
$query = "SELECT menu_name, menu_id FROM menu ORDER BY menu_name ASC";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result
echo "<option value=\"{$row['menu_id']}\
}
mysqli_close($dbc);//close
?>
</select></p>
<input name="menu1" type="radio" value="new" /> new =>
<p>New menu :<input name="menu_name" type="text" /></p>
<p>Price:<input name="price" type="text" /></p>
<p>size:<input name="size" type="text" /></p>
<p>description:<textarea name="description" cols="40" rows="5" /></textarea></p>
</fieldset>
<input name="submit" type="submit" value="submit" />
<input name="submitted" type="hidden" value="TRUE" />
</form>
<?php
}
?>
</body>
</html>
ASKER
There is one database called ecomms
there are 5 tables customer, menu, order_contens, orders and products.
in this script only the menu and products are used.
menu table
key menu_id int(13) unisigned (null no) auto_increment
menu_name varchar(20)
prducts
key product_id int(4) unisigned (null no) auto_increment
menu_id int(3) unisigned (null no)
product_name varchar(60) (null no)
price decimal(6,2) (null no)
size varchar(255) (null yes)
image_name varchar(3) (null no)
the user sees a form on the screen and adds the details for the product and what menu item that product is under.
The script should pull the existing menu items and display them in a select box -- it doesn't do this please help.
also it writes the new menu item and product details to there respected tables.
it does write the menu item but it doesn't write the product details to the product page.
this is the main problem where i need a more experience eye to see whats wrong.
thanks
there are 5 tables customer, menu, order_contens, orders and products.
in this script only the menu and products are used.
menu table
key menu_id int(13) unisigned (null no) auto_increment
menu_name varchar(20)
prducts
key product_id int(4) unisigned (null no) auto_increment
menu_id int(3) unisigned (null no)
product_name varchar(60) (null no)
price decimal(6,2) (null no)
size varchar(255) (null yes)
image_name varchar(3) (null no)
the user sees a form on the screen and adds the details for the product and what menu item that product is under.
The script should pull the existing menu items and display them in a select box -- it doesn't do this please help.
also it writes the new menu item and product details to there respected tables.
it does write the menu item but it doesn't write the product details to the product page.
this is the main problem where i need a more experience eye to see whats wrong.
thanks
ASKER
i have fixed it
i was using the new mysql $result format in places and the old one in others and my versions only like the old one.
thanks for lookiing anyway.
i'm trying to make a ecommerce shopping cart with some simple admin, any help or advice would be great.
x
i was using the new mysql $result format in places and the old one in others and my versions only like the old one.
thanks for lookiing anyway.
i'm trying to make a ecommerce shopping cart with some simple admin, any help or advice would be great.
x
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
- The data you insert
- The expected results
- The actual results
- The exact error message that is presented