php - insert foreign key value from parent table into child table

865

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

665

Answer

Solution:

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

People are also looking for solutions to the problem: php - How to configure scratch application in my local system (windows 7) ??

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.