0

我想根据从下拉列表中选择的值将数据插入表中。其实我有两张桌子,CategoriesProducts。两者都与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('','','','','','','','','');
    }
   ?> 

请建议怎么做?

4

1 回答 1

2
$sql = "INSERT INTO Products (ProductName, ProductCode, ...) VALUES ('".$ProductName."', '". $ProductCode ."', ...";

这就是您使用插入查询的方式。

http://www.tizag.com/mysqlTutorial/mysqlinsert.php

于 2012-05-25T07:24:11.020 回答