在PhpMyAdmin中查询工作,但在PHP代码

时间:2017-02-26 06:34:53

标签: php mysql api mysqli

我写了一个代码,一次插入两个表中,表与外键相互连接。 我在这个网站搜索但没有找到答案。 这个查询在phpmyadmin工作,但在PHP代码中有错误的答案,不能在PHP中工作....为什么??? :( 这是我在php中的查询和代码....请帮帮我

INSERT INTO `interviewdeliverytable` ( `FirstName` , `LastName` , `PhoneNumber` , `PostCode` , `IdNumebr` , `BirthDate` , `Address` , `Accepted` , `IsBike` , `Date` )
VALUES ('$FirstName', '$LastName', '$PhoneNumber', '$PostCode', '$IdNumber', '$BirthDate', '$Address', '$Accepted', '$IsBike', '$Date');
SELECT @Var := Last_INSERT_ID() ;
INSERT INTO `interviewbiketable` (`PhoneNumber` , `DriverLicenseId` , `BikeModel` , `PelakNumebr` ,`Accepted`,`IsBike`, DeliveryId )
VALUES ('$PhoneNumber', '$DeliverLicenseId', '$BikeModel', '$PelakNumber','$Accepted','$IsBike', @Var)

php code

function registerDelivery(){
$connection=createConnection();
if(!$connection){
    echo "Error";
}else{
//
//get objects
//
$json = file_get_contents('php://input');
$obj = json_decode($json);
$FirstName=$obj->FirstName;
$LastName=$obj->LastName;
$PostCode=$obj->PostCode;
$IdNumber=$obj->IdNumebr;
$BirthDate=$obj->BirthDate;
$Address=$obj->Address;
$Accepted=$obj->Accepted;
$IsBike=$obj->IsBike;
$Date=$obj->Date;
$DeliverLicenseId=$obj->DriverLicenseId;
$BikeModel=$obj->BikeModel;
$PelakNumber=$obj->PelakNumebr;
$PhoneNumber=$obj->PhoneNumber;
//
//insert Query
//
$result=mysqli_query($connection,"INSERT INTO `interviewdeliverytable` ( `FirstName` , `LastName` , `PhoneNumber` , `PostCode` , `IdNumebr` , `BirthDate` , `Address` , `Accepted` , `IsBike` , `Date` )
VALUES ('$FirstName', '$LastName', '$PhoneNumber', '$PostCode', '$IdNumber', '$BirthDate', '$Address', '$Accepted', '$IsBike', '$Date');
SELECT @Var := Last_INSERT_ID() ;
INSERT INTO `interviewbiketable` (`PhoneNumber` , `DriverLicenseId` , `BikeModel` , `PelakNumebr` ,`Accepted`,`IsBike`, DeliveryId )
VALUES ('$PhoneNumber', '$DeliverLicenseId', '$BikeModel', '$PelakNumber','$Accepted','$IsBike', @Var)");
echo json_encode(array('Result'=>$result));
mysqli_close($connection);

} }

2 个答案:

答案 0 :(得分:1)

这不是查询,而是多个查询的集合。因此,您必须逐个单独运行它们:

mysqli_query($connection,"INSERT INTO ...");
$id = mysqli_insert_id($connection);
mysqli_query($connection,"INSERT INTO ...");

请注意,您应该为查询使用预准备语句。

答案 1 :(得分:0)

谢谢大家 我编辑了我的查询...

function registerDelivery(){
$connection=createConnection();
if(!$connection){
    echo "Error";
}else{
//
//get objects
//
$json = file_get_contents('php://input');
$obj = json_decode($json);
$FirstName=$obj->FirstName;
$LastName=$obj->LastName;
$PostCode=$obj->PostCode;
$IdNumber=$obj->IdNumebr;
$BirthDate=$obj->BirthDate;
$Address=$obj->Address;
$Accepted=$obj->Accepted;
$IsBike=$obj->IsBike;
$Date=$obj->Date;
$DeliverLicenseId=$obj->DriverLicenseId;
$BikeModel=$obj->BikeModel;
$PelakNumber=$obj->PelakNumebr;
$PhoneNumber=$obj->PhoneNumber;
//
//insert Query
//
$result=mysqli_query($connection,"INSERT INTO `interviewdeliverytable` ( `FirstName` , `LastName` , `PhoneNumber` , `PostCode` , `IdNumebr` , `BirthDate` , `Address` , `Accepted` , `IsBike` , `Date` )
VALUES ('$FirstName', '$LastName', '$PhoneNumber', '$PostCode', '$IdNumber', '$BirthDate', '$Address', '$Accepted', '$IsBike', '$Date');");
 if($result){
     printf ("New Record has id %d.\n", $connection->insert_id);
     $id=$connection->insert_id;
      if($id!=null){
          $resFinall=mysqli_query($connection,
          "INSERT INTO `interviewbiketable` (`PhoneNumber` , `DriverLicenseId` , `BikeModel` , `PelakNumebr` ,`Accepted`,`IsBike`, DeliveryId ) VALUES ('$PhoneNumber', '$DeliverLicenseId', '$BikeModel', '$PelakNumber','$Accepted','$IsBike', '$id');");
           echo json_encode(array('Result'=>$resFinall));
      }}

它工作并将所有数据插入两个表中...非常感谢