Reputation: 5
I am trying to update 2 tables on my database using 1 form. There are a couple of things that happen before the queries execute:
The issue is with the cutomerId that is passed from the form. If the cutomerId is passed from the form (meaning they exist) the second query works. If they don't exist then nothing is passed but the first query creates a new customer hence generating a new customerId.
So my question is how do I get the customerId from the first query and use it in the second? I have all the code but hopefully I have explained the problem enough that the queries themselves will be enough. Many thanks!
$query1 = "INSERT INTO customer (customerId, name, address, phoneNum)" .
"VALUES ( '$customerId','$name', '$address', '$phoneNum')" .
"ON DUPLICATE KEY UPDATE name='$name', address='$address', phoneNum='$phoneNum'";
$result = mysqli_query($dbc, $query1)
or die('Error adding new customer.');
//edit as per Jessica
$customerId = mysqli_insert_id($dbc);
$query2 = "INSERT INTO job (jobType, carType, customerId, phoneNum, name, address, destAdd, bookingDate, BookingTime)" .
"VALUES ('$jobType', '$carType', '$customerId', '$phoneNum', '$name', '$address', '$destAdd', '$bookingDate', '$bookingTime')";
$result = mysqli_query($dbc, $query2)
or die('Error adding booking.');
Upvotes: 0
Views: 95
Reputation: 7005
$customerId = mysqli_insert_id($dbc);
PS: Why on earth are you inserting raw data in your queries instead of using parameters?
Upvotes: 2