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

时间:2017-02-16 07:51:50

标签: php mysql forms

让我开始说我是php的新手,所以我仍然在努力学习。我在Wordpress上创建了一个表单,我想在一个表上插入值(data_test表,我已经管理过),然后从data_test表中获取所有列(id是自动增量号,名称,电子邮件,产品,数量用户输入)并插入到其他表。我使用这个html代码来表单来解析值:



 <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>
&#13;
&#13;
&#13;

然后这个php取值:

&#13;
&#13;
   <?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/");
            }
        }
    }
}
}
?>
&#13;
&#13;
&#13;

现在它在data_test表上插入除id之外的所有值,我猜它是null,因为它必须关闭php上的第一个插入,然后我必须调用第二个插入(//在data_test_ins表上插入数据)在其他PHP? 但我不确定,有人能帮帮我吗?或者只是指导我做正确的方法。 我开始认为我必须创建两个PHP来解析值并采取第一个表,然后在另一个PHP上插入值? 任何想法都有帮助! : - )

2 个答案:

答案 0 :(得分:1)

你在做什么是不对的。手动向数据库添加id字段的值不是一个好方法。它应该由数据库自动生成。我建议的是,在data_test_ins表中添加另一个字段,例如:test_id,它指向data_test表的id。这是外键的概念。 阅读有关外键here

的概念

您的代码现在是: -

   <?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/");
                }
            }
        }
    }
  }
?>

答案 1 :(得分:0)

您是否尝试将$value->id传递给查询而不是$value

它是一个具有结果集当前行的对象,因此您只应传递此对象的id属性。

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

增加:

  • 停止使用mysql已弃用的库。
  • 如果发布的数据是isset或不是
  • ,则应检查发布的数据

编辑:

您的代码应如下所示:

<?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/");
                }
            }
        }
    }
}
?>