插入到选择与PHP代码组合

时间:2016-07-17 09:15:14

标签: php sql

嗨,大家好,我有一些困难与php代码交互,所以我可以执行以sql格式编写的语句我无法绑定其他变量,因为即时通讯也只是在一个语句中插入select,你可以帮帮我吗?非常感谢提前。

我试图在此代码中尝试做的是当用户尝试注册时,用户名和密码将被插入到account_table中,同时customer_IDno也将被提取以插入account_table作为外国人键,我已经得到了声明,但问题是我不知道如何与这些语句的PHP代码进行交互这里是SQL的代码。希望你能得到很多的帮助

INSERT INTO customer_table 
(customer_firstname , customer_middlename , customer_lastname ,
customer_email , customer_address , customer_contact)
VALUES ('Adrian','Manuel','Abrasaldo','Email','New York',09254872645);

INSERT INTO account_table(
account_username , account_password , customer_IDno) 
SELECT '$username' , '$password' , customer_IDno
FROM customer_table WHERE customer_email = '$email')

2 个答案:

答案 0 :(得分:1)

像此代码:

$sql="INSERT INTO customer_table (customer_firstname ,customer_middlename, 
        customer_lastname , customer_email , customer_address ,
        customer_contact) VALUES ('Adrian','Manuel','Abrasaldo','Email','New York',09254872645)";

        mysqli_query($con, $sql);

       //this sql will generate the id .Save it to the variable Here :$newCustID

       $newCustID = mysqli_insert_id($con); // can be get easily
       if ($newCustID) {
          $customer_IDno = $newCustID;
       }


       $username = $_POST['username'];
       $password = $_POST['password'];

       $account_sql = "INSERT INTO account_table(account_username , account_password , 
           customer_IDno) VALUES( ".$username." , ".$password." , ".$customer_IDno.")";
       mysqli_query($con, $account_sql);

答案 1 :(得分:1)

首先执行此查询

mysqli_query(INSERT INTO customer_table 
(customer_firstname , customer_middlename , customer_lastname ,
customer_email , customer_address , customer_contact)
VALUES ('Adrian','Manuel','Abrasaldo','Email','New York',09254872645),$conn);

然后使用

$id = mysqli_insert_id($conn); // $conn is your connection string

$username = $_POST['username'];
$password = $_POST['password'];

  $account_sql = mysqli_query("INSERT INTO account_table(account_username, account_password,customer_IDno) VALUES( ".$username." , ".$password." , ".$id.")",$conn);