duanrang2627 2017-02-16 07:51
浏览 24
已采纳

我如何解析从一个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>
  • 写回答

2条回答 默认 最新

  • douyangqian5243 2017-02-16 07:58
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型