I want to insert my data into a table depending on the value I select from the drop down. Actually I have two tables, Categories
and Products
. Both are interlinked with CategoryID
.
Now I have a form through this I am trying to add a product. I have a dropdown for categories. I will select a category from the dropdown and then I will add related products to that category.
After getting the form data, I fetched my table for CategoryID
like this:
include('includes/conn.php');
if (isset($_POST['submit']))
{
$CategoryName = $_POST['cat'];
echo $CategoryName;
$ProductName = mysql_real_escape_string(htmlspecialchars($_POST['ProductName']));
$ProductCode = $_POST['ProductCode'];
$Specification = $_POST['Specification'];
$Description = $_POST['Description'];
$CostPrice = $_POST['CostPrice'];
$DisplayPrice = $_POST['DisplayPrice'];
$ProductID = $_POST['ProductID'];
$Productimage = $_POST['ProductImage'];
$sql = "select * Categories";
$result = mysql_query ($sql);
$row = mysql_fetch_array($result);
$Category_ID = $row['CategoryID'];
after this I am not getting how to do. Except that condition my code inserts the record successfully. my complete code withouth selecting the categoryid like this
<?php
}
include('includes/conn.php');
if (isset($_POST['submit']))
{
$ProductName = mysql_real_escape_string(htmlspecialchars($_POST['ProductName']));
$ProductCode = $_POST['ProductCode'];
$Specification = $_POST['Specification'];
$Description = $_POST['Description'];
$CostPrice = $_POST['CostPrice'];
$DisplayPrice = $_POST['DisplayPrice'];
$ProductID = $_POST['ProductID'];
$Productimage = $_POST['ProductImage'];
if ($ProductName == '' || $ProductCode == ''|| $Specification == '' || $Description == '' || $CostPrice == '' || $DisplayPrice =='')
{
echo "Please fill in all required fields";
renderForm($ProductID, $ProductName, $ProductCode, $Description, $Specification, $CostPrice, $DisplayPrice, $error);
}
else
{
$sql = "INSERT into Products SET ProductName='$ProductName', ProductCode='$ProductCode', Specification ='$Specification', Description = '$Description', CostPrice = $CostPrice, DisplayPrice = $DisplayPrice, ProductImage = '$ProductImage'";
mysql_query($sql) or die(mysql_error());
echo " Successfully Added ";
//header("Location: view.php");
}
}
else
{
renderForm('','','','','','','','','');
}
?>
Please suggest how to do it?
$sql = "INSERT INTO Products (ProductName, ProductCode, ...) VALUES ('".$ProductName."', '". $ProductCode ."', ...";
This is how you use an insert query.