I haven't much experience with database and php work and have a feeling my code below is a little crappy.
It's basically a form handler for adding a new item to the database. One of the form fields is a drop down list which contains the product category that the new item will be within.
I am currently creating a separate query to get the product category id by using the product category name as a reference for the query in order for the add item query to insert the item under the correct product category. I hope that sort of makes sense!
Here's my code:
Code: Select all
<?php
// get form data
$item_name = $_GET['name'];
$item_desc = $_GET['description'];
$item_price = $_GET['price'];
$item_product = $_GET['product'];
// connect to db
$con = mysql_connect("shares","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// select db
mysql_select_db("dbname", $con);
// Get prod_id for selected product name
// *this is the bad code I think!*
$sql = mysql_query("SELECT prod_id FROM product WHERE name='$item_product'")
or die(mysql_error());
// set $item_product as prod_id
while($row = mysql_fetch_array($sql)) {
$item_product = $row['prod_id'];
echo $item_product;
}
// end bad code!
// insert new record into database
mysql_query("INSERT INTO `item` ( `item_id` , `name` , `desc` , `price` , `prod_id` )
VALUES ('', '$item_name', '$item_desc', '$item_price', '$item_product')");
mysql_close($con);
?>
Dave