我想将数据插入表中,具体取决于我从下拉列表中选择的值。
实际上我有两个表,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('','','','','','','','','');
}
?>
请建议怎么做?
答案 0 :(得分:2)
$sql = "INSERT INTO Products (ProductName, ProductCode, ...) VALUES ('".$ProductName."', '". $ProductCode ."', ...";
这是您使用插入查询的方式。