我想根据从下拉列表中选择的值将数据插入表中。其实我有两张桌子,Categories
和Products
。两者都与CategoryID
.
现在我有一个表格,我正在尝试添加一个产品。我有一个类别的下拉列表。我将从下拉列表中选择一个类别,然后将相关产品添加到该类别中。
获取表单数据后,我获取了我的表格,CategoryID
如下所示:
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'];
在此之后我不知道该怎么做。除了那个条件我的代码成功插入记录。我的完整代码没有像这样选择categoryid
<?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('','','','','','','','','');
}
?>
请建议怎么做?