drrvnbwle80177811 2015-08-09 23:47
浏览 25
已采纳

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

I am trying to insert data from a form in 2 separate tables which are linked by clientID, however I am getting an error:

INSERT INTO client_details (clientID, name, email, address, mobile) VALUES ('', 'a a', 'a', 'a', 'a');INSERT INTO bookings (bookingID, apartmentID, clientID, date_from, date_to, nights, pax, remarks) VALUES ('', '1', LAST_INSERT_ID(), '2015-08-28', '2015-08-31', '3', '4', 'Extra Remarks'); You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO bookings (bookingID, apartmentID, clientID, date_from, date_to, nights' at line 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);
  }
  ?>

GOAL SQL:

BEGIN; INSERT INTO client_detalis (clientID, name, email, address, mobile) VALUES('', '$name $surname', '$email', '$address', '$mobile'); INSERT INTO bookings (bookingID, apartmentID, clientID, date_from, date_to, nights, remarks, pax)) VALUES('', $apartment, LAST_INSERT_ID(),'$booking_from', '$booking_to', '$nights', '$pax', '$remarks'); COMMIT;

  • 写回答

2条回答 默认 最新

  • dousonghs58612 2015-08-10 10:55
    关注

    My answer:

    <?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
    ", $conn->error);
     }
    
     if (!$conn->query("INSERT INTO client_details (clientID, name, email, address, mobile) VALUES ('', '$name $surname', '$email', '$address', '$mobile')")) {
         printf("Errormessage: %s
    ", $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
    ", $conn->error);
     }
    // important to insert as code will not work without commit
     if (!$conn->commit()) {
         printf("Errormessage: %s
    ", $conn->error);
     }
     $conn->close();
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误