Using MySql and phpmyadmin, I have two tables person
and report
which are linked using the field person_id
as a foreign key in the report
table. Using one php form I am trying to add a persons details to the person
table. Using the same form I want it to post the date to the report
table which will then generate an auto increment report_id
and link that person with a specific report on that date using the person_id
foreign key.
Here is my current code
<?php
if (isset($_POST['submitted'])) {
include('dbcon.php'); //link to connection file
$pid = "SELECT person_id FROM person
"
. "ORDER BY person_id DESC
"
. "LIMIT 1"; //variable finds last generated person_id
$sqlinsert1 = "INSERT INTO person (person.title, person.first_name, person.last_name, person.address, person.contact_no, person.email, person.ha_id) VALUES ('$_POST[title]' , '$_POST[first_name]' , '$_POST[last_name]' , '$_POST[address]' , '$_POST[contact_no]' , '$_POST[email]' , '$_POST[ha_id]')";
$sqlinsert2 = "INSERT INTO report (report.date) VALUES ('$_POST[date]') WHERE ($pid = report.person_id)";
if (!mysqli_query($dbcon, $sqlinsert1)) {
die('Error inserting record');
} //end of nested if statement
if (!mysqli_query($dbcon, $sqlinsert2)) {
die('Error inserting record');
} //end of nested if statement
$newrecord = "new record added to database";
} //end of if statement
?>
I have created the variable $pid
which will find the last person_id
generated in the person
table, I have tested this and it works in phpmyadmin. I want to then use this variable to link the date with the person_id
and place it into the report
table.
This may sound quite complicated but I'm sure there is an easy answer.