I'd like to ask. I have two tables in a database. Here's the case, I have a form to get all data needed to be stored. Some of these data should have to be stored in one table, and some to the other. But, the ID of the data in one table should also be stored in another table for retrieving purposes. For example, in table 1 I have ID, this ID at table one should also be stored at the same time in table 2 as T2_ID, the moment the user pressed the Submit button, the data should automatically be stored evenly according to its purpose. However, in my case, only those data for table 1 are stored because I couldn't retrieve the ID from table 1 after it has been saved.
Here's my code:
if (mysqli_query($connect, $sql)) {
$getinfo = "SELECT id FROM names WHERE firstname='$firstname' AND lastname='$lastname'";
$query = mysql_query($getinfo, $connect);
$row = mysql_fetch_array($query);
$forid = $row['id'];
$sql2 = "INSERT INTO phonenumbers (name_id, type, number)
VALUES ('$forid', '$contacttype', '$newnum')";
if (mysqli_query($connect, $sql2)) {
echo "<script>alert('New record added successfully!');</script>";
header('Location: index.php');exit();
}
else {
echo "<script>alert('Error: " . $sql2 . "<br>" . mysqli_error($connect) . "');</script>";
header('Location: index.php');exit();
}
}
else {
echo "<script>alert('Error: " . $sql . "<br>" . mysqli_error($connect) . "');</script>";
header('Location: index.php');exit();
}