Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据id向数据库中插入值_Php_Mysql - Fatal编程技术网

Php 根据id向数据库中插入值

Php 根据id向数据库中插入值,php,mysql,Php,Mysql,我想根据从下拉列表中选择的值将数据插入表中。 实际上我有两个表,类别和产品。两者都与类别ID链接 现在我有了一个表单,通过这个表单我试图添加一个产品。我有一个类别的下拉列表。我将从下拉列表中选择一个类别,然后将相关产品添加到该类别中 获取表单数据后,我获取了CategoryID的表,如下所示: include('includes/conn.php'); if (isset($_POST['submit'])) { $CategoryName = $_POST['cat'];

我想根据从下拉列表中选择的值将数据插入表中。 实际上我有两个表,
类别
产品
。两者都与
类别ID
链接
现在我有了一个表单,通过这个表单我试图添加一个产品。我有一个类别的下拉列表。我将从下拉列表中选择一个类别,然后将相关产品添加到该类别中

获取表单数据后,我获取了
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'];
这之后我就不知道该怎么办了。除此之外,我的代码成功地插入了记录。 不选择类别的完整代码如下

<?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('','','','','','','','','');
    }
   ?> 

这就是如何使用插入查询


确保结合使用sprintf和mysql\u real\u escape\u string()来屏蔽数据库是的,我已经更正了插入查询,但这不是我的问题。我想在cateoryid存在的地方插入数据
$sql = "INSERT INTO Products (ProductName, ProductCode, ...) VALUES ('".$ProductName."', '". $ProductCode ."', ...";