我如何解析从一个PHP到另一个PHP的值

Let's start that I am newbie in php, so still I am trying to learn. I have created a form on Wordpress and I want to insert the values on one table (data_test table, i have managed that) and then take all the columns from data_test table(id that is auto increment number,name,email,product, quantity that the user enter) and insert to other table. I used this html code for the form to parse the values:

 <form action="../enter_data_insert.php" method="post" onsubmit="return form_validation()" name="myForm">

        Name <input id="name" name="name" type="text" />

        Email <input id="email" name="email"  required  type="email"/>

        Product<input id="prod" name="prod"  required type="text" />

        Quantity<input id="quant" name="quant"  required type="number"  min="1"  / >

        <input type="submit" value="Submit" />

        </form>

And then this php to take the values:

   <?php 

if(!empty($_POST["name"]) && !empty($_POST["email"]) && !empty($_POST["prod"]) && !empty($_POST["quant"])){

//connect with database
include "database_conn.php";

//get the form elements and store them in variables

session_start();
$name=$_POST["name"]; 
$email=$_POST["email"]; 
$prod=$_POST["prod"]; 
$quant=$_POST["quant"]; 

//insert data on data_test table
$sql="INSERT INTO `site_db`.`data_test` ( `name` , `email`, `prod`,`quant`) VALUES ( '$name','$email','$prod','$quant')"; 
if(!mysqli_query($con,$sql)){
    echo mysqli_error($con);
} else{

    //retrieve data 

    $sql = "SELECT data_test_id FROM data_test WHERE prod='$prod'";
    $result = mysqli_query($con,$sql);
    if(!$result){
        echo mysqli_error($con);
    } else{
        while($value = mysqli_fetch_object($result)){
            $id = intval($value->id);
            $_SESSION['myid'] = $value->id;
            var_dump($value);
            
            //insert data on data_test_ins table
           $sql="INSERT INTO site_db.data_test_ins` ( id,name , email, prod,quant) VALUES ( $id,'$name','$email','$prod','$quant')";
            if(!mysqli_query($con,$sql)){
                echo mysqli_error($con);
            } else{

                //Redirects to the specified page
             //   header("Location: http://localhost/site/");
            }
        }
    }
}
}
?>

Now it inserts all the values except the id on data_test table, i guess that it is null because it must close the first insert on php and then i have to call a second insert (with //insert data on data_test_ins table) on other php? But i am not sure, can anyone help me please? or just guide me what is the right way to do. I start to think that i have to create two php to parse the values and take on the first table and then on the other php to insert the values? Any thoughts are helpful! :-)

</div>

have you tried passing $value->id into the query instead of $value?

its an object which has the current row of a result set, so you should only pass the id attribute of this object.

$sql="INSERT INTO `site_db`.`data_test_ins` ( `id`,`name` , `email`, `prod`,`quant`) VALUES ( '$value->id','$name','$email','$prod','$quant')";

Addition:

  • stop using the mysql deprecated library.
  • you should check the posted data if its isset or not

EDIT:

your code should looks like:

<?php 

if(!empty($_POST["name"]) && !empty($_POST["email"]) && !empty($_POST["prod"]) && !empty($_POST["quant"])){

    //connect with database
    include "database_conn.php";

    //get the form elements and store them in variables

    session_start();
    $name=$_POST["name"]; 
    $email=$_POST["email"]; 
    $prod=$_POST["prod"]; 
    $quant=$_POST["quant"]; 

    //insert data on data_test table
    $sql="INSERT INTO `site_db`.`data_test` ( `name` , `email`, `prod`,`quant`) VALUES ( '$name','$email','$prod','$quant')"; 
    if(!mysqli_query($con,$sql)){
        echo mysqli_error($con);
    } else{

        //retrieve data 

        $sql = "SELECT data_test_id FROM data_test WHERE prod='$prod'";
        $result = mysqli_query($con,$sql);
        if(!$result){
            echo mysqli_error($con);
        } else{
            while($value = mysqli_fetch_object($result)){
                
                $_SESSION['myid'] = $value->data_test_id;
                $id = intval($value->data_test_id);
                
                
                //insert data on data_test_ins table
               $sql="INSERT INTO `site_db`.`data_test_ins` ( id,name , email, prod,quant) VALUES ( '$id','$name','$email','$prod','$quant')";
                if(!mysqli_query($con,$sql)){
                    echo mysqli_error($con);
                } else{

                    //Redirects to the specified page
                   header("Location: http://localhost/site/");
                }
            }
        }
    }
}
?>

</div>

What you are doing is not right. It is not a good approach to add value to id field to the database manually. It should be generated automatically by the database. What I would recommend is, add another field to your data_test_ins table eg: test_id which points to the id of your data_test table. This is the concept of foreign key. Read about the concept of foreign keys here

Your code would now be:-

   <?php 

if(!empty($_POST["name"]) && !empty($_POST["email"]) && !empty($_POST["prod"]) && !empty($_POST["quant"])){

    //connect with database
    include "database_conn.php";

    //get the form elements and store them in variables

    session_start();
    $name=$_POST["name"]; 
    $email=$_POST["email"]; 
    $prod=$_POST["prod"]; 
    $quant=$_POST["quant"]; 

    //insert data on data_test table
    $sql="INSERT INTO `site_db`.`data_test` ( `name` , `email`, `prod`,`quant`) VALUES ( '$name','$email','$prod','$quant')"; 
    if(!mysqli_query($con,$sql)){
        echo mysqli_error($con);
    } else{

        //retrieve data 

        $sql = "SELECT data_test_id FROM data_test WHERE prod='$prod'";
        $result = mysqli_query($con,$sql);
        if(!$result){
            echo mysqli_error($con);
        } else{
            while($value = mysqli_fetch_object($result)){
                $id = $value->id;

                //insert data on data_test_ins table
                $sql="INSERT INTO `site_db`.`data_test_ins` ( `id`,`name` , `email`, `prod`,`quant`, `test_id`) VALUES ('$name','$email','$prod','$quant', '$id')"; 
                if(!mysqli_query($con,$sql)){
                    echo mysqli_error($con);
                } else{

                    //Redirects to the specified page
                    header("Location: http://localhost/site/");
                }
            }
        }
    }
  }
?>