如何从下拉列表中捕获主键并插入另一个表的外键?

时间:2014-11-05 10:53:10

标签: php mysql

请帮助我评论一些用于测试的东西,但没有任何作用

<?php
    //retrieve the data sent in  the POST request

         $yourDateOrdered =$_POST["DateOrdered"];
         $yourDueDate = $_POST["DueDate"];
         if(isset($_POST["CompanyName"])){$yourCompanyName = $_POST["CompanyName"];}


    //Validate the fields   
        if ($yourDateOrdered=="" || $yourDateOrdered==null){
                $err= $err."Please enter the date the purchase order was made<br>";
             }  

        if ($yourDueDate=="" || $yourDueDate==null){
            $err= $err. "Please enter a date when the item is required<br>";
            }

        //if ($yourCompanyName=="" || $yourCompanyName==null){
            //$err= $err."Please enter the customer name<br>";
             //}            

    //Connect to the server and select database
        include("dbConnection.php");

    //define sql query to execute on the database
        $Query1="INSERT INTO orders(CompanyName, DateOrdered, DueDate)
        VALUES ('$yourCompanyName','$yourDateOrdered', '$yourDueDate')";

        //execute query
        //$result = mysql_query($Query1);

            //echo("The following order has been added");


    //result of the action stored in $Result
        $Result = mysql_query($Query1);

        if($Result){
            echo 'Order entered';
            echo Header ("Location:orderformitem.php");

            } 

    //Close the connection
        mysql_close($con);  

    //Check if query executed successfully and forward the user to an appropriate location
        //if($queryResult){
            //echo "Order save <br>";
                //Header ("Location:../PHP/orderformitem.php");
                //}
?>

1 个答案:

答案 0 :(得分:0)

  • 您肯定需要学习如何调试。首先,注释掉Header('Location ...');行,以捕获错误。

  • 在文件顶部添加error_reporting(E_ALL);display_errors(1);,以查看错误。

  • var_dump($_POST)看看,所有变量都是正确的。

  • 如果您想要正确的日期,请进行日期验证。

  • 转储您的查询,并尝试直接在sql中运行它。

  • 不要使用mysql函数,因为它们已被弃用。请改用mysqli或PDO。

  • 逃避您的数据,以避免sql注入!