SITUATION: I have 3 tables: people (peopleID, firstName, lastName), phoneNumbers (phoneID, countryCode, areaCode, phoneNumber, extension) and peoplePhoneNumbers (peopleID, phoneID) (which is a junction table with foreign keys for manyTOmany relation).
Everything is updated using one form.
GOAL: Let's say I have: John Smith 201 1111111 23, if I enter: Paul Lennon 201 1111111 23 (same phone number) the system should: 1) create a new row in table people (with new ID) 2) DON'T create a new row in table phone and return the ID already used for that same phone number. (2 phone number to be the same they should have same area code, same number and same extension).
This is the code I have so far:
mysqli_query($link, "SET AUTOCOMMIT=0") or die(mysqli_error($link));
mysqli_query($link,"START TRANSACTION") or die(mysqli_error($link));
$sql1 = "INSERT INTO tbl_people (firstname, lastname)
VALUES ('$firstname', '$lastname');";
$sql2 ="INSERT INTO tbl_PhoneNumbers (AreaCode, PhoneNumber)
VALUES ('$areamobile', '$mobile');";
mysqli_query($link, $sql1) or die('Error: ' . mysqli_error($link));
$last_people_id = mysqli_insert_id($link);
mysqli_query($link, $sql2) or die('Error: ' . mysqli_error($link));
$last_phone_id = mysqli_insert_id($link);
$sql3 ="INSERT INTO tbl_peoplephonenumbers (peopleID, phoneID)
VALUES ('$last_people_id', '$last_phone_id');";
mysqli_query($link, $sql3) or die(mysqli_error($link));
header("Location: index.php?act=edit&people_id=$last_people_id"); /*after adding new person goes to edit page*/
mysqli_query($link, "COMMIT") or die(mysqli_error($link));
mysqli_query($link, "SET AUTOCOMMIT=1") or die(mysqli_error($link));