简体   繁体   English

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

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

Please help i commented off some stuff for testing purposes but nothing works 请帮助我评论掉一些东西用于测试目的,但没有任何效果

<?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");
                //}
?>
  • You definietly need to learn how to debug. 您绝对需要学习如何调试。 First, comment out the Header('Location ...'); 首先,注释掉Header('Location ...'); row, to catch errors. 行,以捕获错误。

  • add error_reporting(E_ALL); 添加error_reporting(E_ALL); and display_errors(1); display_errors(1); at top of your file, to see any errors. 在文件顶部,以查看所有错误。

  • Let's var_dump($_POST) to see, is all the variables are correct. 让我们用var_dump($_POST)看看,是否所有变量都是正确的。

  • Do a date validation, if you are want correct dates. 如果您想要正确的日期,请进行日期验证。

  • Dump your query, and try to run it in sql directly. 转储您的查询,并尝试直接在sql中运行它。

  • DO NOT use mysql functions because they are deprecated. 不要使用mysql函数,因为它们已被弃用。 Use mysqli or PDO instead. 改用mysqli或PDO。

  • Escape your data , to avoid sql injections! 转义数据 ,以避免sql注入!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 php mysql 在另一个表中插入选定下拉列表值的主键作为外键 - Insert Primary key of Selected Drop down list value as a foreign key in another table using php mysql 如何将主键作为外键插入另一个表? - How do I insert the primary key to another table as foreign key? 尝试将下拉列表中的数据作为外键插入另一个表中。 数据未插入 - Trying to insert data from a drop-down list as a foreign key in another table. Data not inserted 如何从另一个表主键添加mysql外键? - How to Add mysql foreign key from another table primary key? 如何将数据插入一个表并获取主键作为外键插入另一个表中? - How do I insert data into one table & get the primary key to insert in another table as a foreign key? MYSQL在另一个表中插入主键作为外键 - MYSQL Insert a primary key in another table as foreign key 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another 如何使用php和mysql将外键字段链接到另一个表的主键字段? - How to link the foreign key field to a primary key field from another table using php and mysql? 来自外键的PHP主键表 - PHP primary key table from foreign key 在另一个表的主键;外键列中插入记录 - Inserting records in a primary;foreign key columns from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM