简体   繁体   中英

Select from a table and insert into another table

1- I have inserted products in the table cart (and it works)
2- I identify if is a new or old customer(and it works)
3- I insert the new customer in the table customers or I update the new info of an existed customer. (and it works) I write here the code simplified.

I suppose that now I need to insert in the table cart, row customer_id, the id just created in the table customers (I mean the session id of table customers) How can I do that?

if(isset($_POST["continue"])){
    mysql_query("INSERT INTO customers (name)
    VALUES ('$_POST[name]')") 
    or die("error:".mysql_error());
    header("Location:step4.php");   

}else if (isset($_POST["update"])) {
    mysql_query("UPDATE customers 
                 SET name='$_POST[name]'
                 WHERE mail='$_SESSION[mail]'")
    or die("error:".mysql_error());
    header("Location:step4.php?updated");   
}

I tried and it does not work this:

mysql_query("INSERT INTO cart (customer_id)
             SELECT'$_SESSION['id']' 
             FROM customers")

使用此处记录的mysql_insert_id()http : //php.net/manual/zh/function.mysql-insert-id.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM