1

Hi guys I having some difficulties interacting with php code so I can execute the statement written in sql form i can't bind the other variables because im also doing some insert into select in just one statement can you help me out? thanks a lot in advance .

What im trying to do in this code is when a user is trying to sign up there username and password will be inserted into the account_table and at the same time the customer_IDno will be also fetch to be inserted in account_table to be a foreign key, I already got the statement but the problem is I don't know how to interact with the php code with these kind of statement here's the code for the SQL. hope you'll help thanks a lot

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')
7
  • you are first inserting into customer_table and then inserting into account table right. Commented Jul 17, 2016 at 9:25
  • yes that's what im doing in that statement customer_id will be fetch from customer_table after the user sign up then the username and password will be inserted with the customer_IDno at the same time but im having difficulties with the php code Commented Jul 17, 2016 at 9:39
  • $username and password field is not present in customer_table then it can not be selected from the table ,It will be a $_POST values Commented Jul 17, 2016 at 9:45
  • yes exactly i have already tried that in phpmyadmin and it works very well my only problem is how to interact with php code because it has select inside the insert into Commented Jul 17, 2016 at 10:11
  • there is no need to select in when inserting to account table $suername and $password can be get from $_POST and customer_id can be get from as last_insert_id when inserting is made to customer table . So this way we have alll . now insert into the account table Commented Jul 17, 2016 at 10:25

2 Answers 2

1

First execute this query

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

then use

$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);
Sign up to request clarification or add additional context in comments.

Comments

1

Something like This Code :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.