如何在多个表中插入数据mysqli

时间:2015-08-09 23:47:12

标签: php mysqli insert

我试图将表单中的数据插入2个由clientID链接的单独表中,但是我收到错误:

  

INSERT INTO client_details(clientID,name,email,address,mobile)VALUES('',' a',' a',&#39 ; a',' a'); INSERT INTO预订(bookingID,apartmentID,clientID,date_from,date_to,nights,pax,remarks)VALUES('',' ; 1',LAST_INSERT_ID(),' 2015-08-28',' 2015-08-31',' 3',' 4& #39;,'额外备注');   您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在预订附近使用{bookingID,apartmentID,clientID,date_from,date_to,night'在第1行

<?php
include 'connect.php';

    $apartment = (isset($_POST['apartment']) ? $_POST['apartment'] : null);
    $name = (isset($_POST['name']) ? $_POST['name'] : null);
    $surname = (isset($_POST['surname']) ? $_POST['surname'] : null);
    $email = (isset($_POST['email']) ? $_POST['email'] : null);
    $address = (isset($_POST['address']) ? $_POST['address'] : null);
    $mobile = (isset($_POST['mobile']) ? $_POST['mobile'] : null);
    $pax = (isset($_POST['pax']) ? $_POST['pax'] : null);
    $address = (isset($_POST['address']) ? $_POST['address'] : null);
    $remarks = (isset($_POST['remarks']) ? $_POST['remarks'] : null);
    $day_from = (isset($_POST['day_from']) ? $_POST['day_from'] : null);
    $month_from = (isset($_POST['month_from']) ? $_POST['month_from'] : null);
    $year_from = (isset($_POST['year_from']) ? $_POST['year_from'] : null);
    $booking_from = $year_from."-".$month_from."-".$day_from;
    $day_to = (isset($_POST['day_to']) ? $_POST['day_to'] : null);
    $month_to = (isset($_POST['month_to']) ? $_POST['month_to'] : null);
    $year_to = (isset($_POST['year_to']) ? $_POST['year_to'] : null);
    $booking_to = $year_to."-".$month_to."-".$day_to;
    $no_of_nights = abs(strtotime($booking_to) - strtotime($booking_from));     
    $days = floor($no_of_nights / (60*60*24));

    $sql = "INSERT INTO client_details (clientID, name, email, address, mobile) VALUES ('', '$name $surname', '$email', '$address', '$mobile');";
    $sql.= "INSERT INTO bookings (bookingID, apartmentID, clientID, date_from, date_to, nights, pax, remarks) VALUES ('', '$apartment', LAST_INSERT_ID(), '$booking_from', '$booking_to', '$days', '$pax', '$remarks');";

            if (mysqli_query($conn, $sql)) {
                echo "";
            } else {
                echo "" . $sql . "<br>" .mysqli_error($conn);
  }
  ?>

目标SQL:

  

BEGIN;   INSERT INTO client_detalis(clientID,name,email,address,mobile)     价值观(&#39;&#39;,&#39; $ name $ surname&#39;,&#39; $ email&#39;,&#39; $ address&#39;,&#39; $ mobile&# 39);   INSERT INTO预订(bookingID,apartmentID,clientID,date_from,date_to,nights,remarks,pax))     价值观(&#39;&#39;,$ apartment,LAST_INSERT_ID(),&#39; $ booking_from&#39;,&#39; $ booking_to&#39;,&#39; $ nights&#39;,&# 39; $ pax&#39;,&#39; $ remarks&#39;);   COMMIT;

2 个答案:

答案 0 :(得分:1)

我的回答:

<?php

    $apartment = (isset($_POST['apartment']) ? $_POST['apartment'] : null);
    $name = (isset($_POST['name']) ? $_POST['name'] : null);
    $surname = (isset($_POST['surname']) ? $_POST['surname'] : null);
    $email = (isset($_POST['email']) ? $_POST['email'] : null);
    $address = (isset($_POST['address']) ? $_POST['address'] : null);
    $mobile = (isset($_POST['mobile']) ? $_POST['mobile'] : null);
    $pax = (isset($_POST['pax']) ? $_POST['pax'] : null);
    $address = (isset($_POST['address']) ? $_POST['address'] : null);
    $remarks = (isset($_POST['remarks']) ? $_POST['remarks'] : null);
    $day_from = (isset($_POST['day_from']) ? $_POST['day_from'] : null);
    $month_from = (isset($_POST['month_from']) ? $_POST['month_from'] : null);
    $year_from = (isset($_POST['year_from']) ? $_POST['year_from'] : null);
    $booking_from = $year_from."-".$month_from."-".$day_from;
    $day_to = (isset($_POST['day_to']) ? $_POST['day_to'] : null);
    $month_to = (isset($_POST['month_to']) ? $_POST['month_to'] : null);
    $year_to = (isset($_POST['year_to']) ? $_POST['year_to'] : null);
    $booking_to = $year_to."-".$month_to."-".$day_to;
    $no_of_nights = abs(strtotime($booking_to) - strtotime($booking_from));     
    $days = floor($no_of_nights / (60*60*24));

  include 'connect.php';

 if (!$conn->autocommit(FALSE)) {
    printf("Errormessage: %s\n", $conn->error);
 }

 if (!$conn->query("INSERT INTO client_details (clientID, name, email, address, mobile) VALUES ('', '$name $surname', '$email', '$address', '$mobile')")) {
     printf("Errormessage: %s\n", $conn->error);
 }

 if (!$conn->query("INSERT INTO bookings (bookingID, apartmentID, clientID, date_from, date_to, nights, pax, remarks) VALUES ('', '$apartment', LAST_INSERT_ID(), '$booking_from', '$booking_to', '$days', '$pax', '$remarks')")) {
     printf("Errormessage: %s\n", $conn->error);
 }
// important to insert as code will not work without commit
 if (!$conn->commit()) {
     printf("Errormessage: %s\n", $conn->error);
 }
 $conn->close();


?>

答案 1 :(得分:0)

您必须使用mysqli事务方法,如本答案中所述: how-to-start-and-end-transaction-in-mysqli