php中的两个插入语句

时间:2017-01-19 07:29:45

标签: php mysqli

大家好,我如何在php代码中有两个插入语句将数据存储到数据库中的两个不同的表中?

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

2 个答案:

答案 0 :(得分:0)

你应该使用mysqli预备语句和别的

   # Retrieve scroll 
   my $scroll = $self->getScrollBySignature($item);

   # Retrieve all affected documents ids 
   my @allDocs;
   while (my @docs = $scroll->next(500)) {
       push @allDocs, map {$_->{_id}} @docs
   }

   foreach (@allDocs) {
      # Do stuff with doc
   }

答案 1 :(得分:-1)

看看交易可能是什么? 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);

此外,如果交易失败,您可以随时回滚

另外,我建议您使用更多面向对象的方法:

$mysqli = new mysqli("localhost", "user", "password", "world");
$mysqli->query();
$mysqli->query();
$mysqli->close();