0

Hi guys how do I have two insert statements in the php codes to store the data into two different tables in the database?

$order = "INSERT INTO NewCase(CaseID, StaffName, Category, PriorityLevel, Status, Date, Summary, ResidentID)
VALUES (NULL, '$staffname', '$category', '$prioritylevel', '$status', '$checkdate', '$summary', NULL)";

$order .= "INSERT INTO NewResident(ResidentID, NRIC, ResidentName, Telephone, Email, Gender, Street1, Street2, PostalCode)
VALUES (NULL, '$nric', '$residentname', '$telephone', NULL, NULL, '$street1', '$street2', '$postalcode')";

$retval = mysqli_multi_query($link, $order);
helpmepls
  • 47
  • 1
  • 2
  • 11

2 Answers2

1

You should be using mysqli prepared statements and nothing else.

$sql = "INSERT INTO NewCase VALUES (NULL, ?, ?, ?, ?, ?, ?, NULL)";
$stmt = $link->prepare($sql);
$stmt->bind_param("ssssss",$staffname, $category, $prioritylevel, $status, $checkdate, $summary);
$stmt->execute();

$sql = "INSERT INTO NewResident VALUES (NULL, ?,?,?, NULL, NULL, ?,?,?)";
$stmt = $link->prepare($sql);
$stmt->bind_param("ssssss",$nric, $residentname, $telephone, $street1, $street2, $postalcode);
$stmt->execute();
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

Take a look into transactions may be? http://php.net/manual/en/mysqli.begin-transaction.php

mysqli_begin_transaction($link, MYSQLI_TRANS_START_READ_WRITE);

mysqli_query($link, $order1);
mysqli_query($link, $order2);

mysqli_commit($link);

mysqli_close($link);

Also if transaction fail you can always rollback

Also I suggest you use more Object Oriented approach:

$mysqli = new mysqli("localhost", "user", "password", "world");
$mysqli->query();
$mysqli->query();
$mysqli->close();
Vlad K
  • 145
  • 8