简体   繁体   中英

insert foreign key value from parent table into child table

I have two tables Loan(Parent Table) and Receipt(Child table)what i want to do is when a row is inserted in the parent table(loan) i want the foreign key(app_file_id) to also be inserted in the child table

$values = $_POST;
foreach ($values as &$value) {
    $value = mysql_real_escape_string($value);
}

$sql1="INSERT INTO loan (loan_id,officer_department,app_file_id)
VALUES ('','$values[officer_department]','$values[app_file_id]')";

$result = mysql_query($sql1);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

I have managed to insert data in the parent table but the child table (receipt), and the foreign key (app_file_id), how do i get it to be inserted in the receipt table as well

Try this

$sql1="INSERT INTO loan (loan_id,officer_department,app_file_id)
VALUES ('','$values[officer_department]','$values[app_file_id]')";

$result = mysql_query($sql1);

if (!$result) {
            die('Invalid query: ' . mysql_error());
}

@mysql_query("INSERT INTO receipt (app_file_id) VALUES ('{$values['app_file_id']}')");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM