I am building a .php to insert data to two different tables. I already used mysqli_multi_query() function and mysqli_insert_id(), however, only the submitted data only appear in one of the table. I already stuck in this hurdle for a few days, can anyone please help me find out what's wrong with my code.
Here are the 2 tables:
Table patients: patient_id(PK)(AI), name, age, email,mobile.....
Table emergency_contact: id(PK)(AI), patient_id(FK), name, relationship, mobile
<?php
session_start();
?>
<?php
$pname=$_POST['pname'];
$pid=$_POST['pid'];
$hkid=$_POST['hkid'];
$dob=$_POST['dob'];
$age=$_POST['age'];
$gender=$_POST['gender'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
$address=$_POST['address'];
$ename=$_POST['ename'];
$relationship=$_POST['relationship'];
$emobile=$_POST['emobile'];
$con = new mysqli("localhost","root","2013700910","2013700910");
$last_id = $con->insert_id;
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
$sql="INSERT INTO patients (name, hkid, date_of_birth, age, gender, mobile, email, address)
VALUES('$pname', '$hkid' ,'$dob' ,'$age', '$gender', '$mobile', '$email', '$address');
INSERT INTO products (name, relationship, mobile, patient_id)
VALUES('$ename', '$relationship', '$emobile', '$last_id')";
if ($con->multi_query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
$con->close();
?>
(P.S. *only '$pname', '$hkid' ,'$dob' ,'$age', '$gender', '$mobile', '$email', '$address'are successfully inserted into the database. )