插入到选择结合 PHP 代码



嗨,伙计们,我在与 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')

首先执行此查询

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);

像这样的代码:

$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);

最新更新