将父表中的外键值插入子表

时间:2012-04-01 23:12:29

标签: php mysql

我有两个表Loan(父表)和Receipt(子表)我想要做的是当在父表中插入一行(loan)时我希望外键(app_file_id)也插入到儿童桌

$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());
}

我已设法在父表中插入数据,但子表(收据)和外键(app_file_id),如何将其插入到收据表中

1 个答案:

答案 0 :(得分:0)

试试这个

$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']}')");