Fügen Sie zuerst einen neuen Datensatz in die Tabelle tbl_customer
ein und rufen Sie die letzte Einfüge-ID ab. Mit dieser ID fügen Sie den Datensatz in die Tabelle tbl_sales
ein. Angenommen, Sie verwenden pdo
und auto_increment
etwas wie folgt aus:
//create a prepared statement for inserting into the customer table
$prepStmt= $pdo->prepare("INSERT INTO tbl_customer (customer_firstname, customer_address, customer_contact)
VALUES(:firstname, :address, :contact)");
$prepStmt->execute(array(
"firstname" => $_POST['yourFormValueForFirstname'], //TODO: add validation for $_POST variables because of XSS-attacks!
"address" => $_POST['yourFormValueForAddress'],
"contact" => $_POST['yourFormValueForContact']
));
//now get the last inserted id
$lastCustomerId = $pdo->lastInsertId();
//create a prepared statement for inserting into sales table
$prepStmt= $pdo->prepare("INSERT INTO tbl_sales (customer_id,product_name, product_qty, product_price)
VALUES(:customerId, :prodName, :prodQ,:prodPrice)");
//and use the last inserted customer id for the new sales record
$prepStmt->execute(array(
"customerId" => lastCustomerId,
"prodName" => $productName,
"prodQ" => $productQuantity,
"prodPrice" => $productPrice
));
Was ist das Problem, das Sie haben? – eol
Wie sieht es aus, wenn Sie Daten zu MySQL hinzufügen? – Labradorcode
@eol wie man dem Kunden eine ID gibt und gleichzeitig die von ihm bestellten Produkte anbringt – Odie