插入select并与PHP代码结合

插入select并与PHP代码结合,php,sql,Php,Sql,大家好,我在与php代码交互时遇到了一些困难,因此我可以执行以sql形式编写的语句。我不能绑定其他变量,因为我还在一个语句中插入select,您能帮我吗?先谢谢你 我在这段代码中试图做的是,当用户试图在那里注册时,用户名和密码将被插入account_表中,同时customer_ID号也将被提取,以作为外键插入account_表中,我已经得到了这个语句,但问题是我不知道如何用这种语句与php代码交互。下面是SQL的代码。希望你能帮忙,谢谢 INSERT INTO customer_table (

大家好,我在与php代码交互时遇到了一些困难,因此我可以执行以sql形式编写的语句。我不能绑定其他变量,因为我还在一个语句中插入select,您能帮我吗?先谢谢你

我在这段代码中试图做的是,当用户试图在那里注册时,用户名和密码将被插入account_表中,同时customer_ID号也将被提取,以作为外键插入account_表中,我已经得到了这个语句,但问题是我不知道如何用这种语句与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')

类似以下代码的内容:

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

首先执行这个查询

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\u insert\u id($conn);//$conn是您的连接字符串

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

您首先插入customer_表,然后插入account表。是的,这就是我在该语句中所做的。用户注册后,将从customer_表中获取customer_id,然后用户名和密码将与customer_id号同时插入,但我在php代码$username和customer_表中不存在密码字段,因此无法从表中选择该字段,这将是一个$\u POST值确切地说,我已经在phpmyadmin中尝试过了,它工作得很好,我唯一的问题是如何与php代码交互,因为它在insert INTO中有select,插入到account表时不需要选择in,$suername和$password可以从$\u POST获得,而客户id可以从as获得插入时的最后一个插入id是对客户表进行的。这样我们就有了一切。现在插入account表