簡體   English   中英

如何從下拉列表中捕獲主鍵並插入為另一個表的外鍵?

[英]How to capture primary key from drop down and insert as foreign key of another table?

請幫助我評論掉一些東西用於測試目的,但沒有任何效果

<?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");
                //}
?>
  • 您絕對需要學習如何調試。 首先,注釋掉Header('Location ...'); 行,以捕獲錯誤。

  • 添加error_reporting(E_ALL); display_errors(1); 在文件頂部,以查看所有錯誤。

  • 讓我們用var_dump($_POST)看看,是否所有變量都是正確的。

  • 如果您想要正確的日期,請進行日期驗證。

  • 轉儲您的查詢,並嘗試直接在sql中運行它。

  • 不要使用mysql函數,因為它們已被棄用。 改用mysqli或PDO。

  • 轉義數據 ,以避免sql注入!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM