简体   繁体   中英

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 ...'); row, to catch errors.

  • add error_reporting(E_ALL); and 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.

  • Do a date validation, if you are want correct dates.

  • Dump your query, and try to run it in sql directly.

  • DO NOT use mysql functions because they are deprecated. Use mysqli or PDO instead.

  • Escape your data , to avoid sql injections!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM