【问题标题】:How to insert data in multiple tables mysqlimysqli如何在多个表中插入数据
【发布时间】:2015-08-09 23:47:12
【问题描述】:

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

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, comments) VALUES ('', '1', LAST_INSERT_ID(), '2015-08-28', '2015-08-31', '3', '4', '额外备注'); 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“INSERT INTO bookings(bookingID、apartmentID、clientID、date_from、date_to、nights”附近使用正确的语法

<?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:

开始; INSERT INTO client_detalis (clientID, name, email, address, mobile) VALUES('', '$name $surname', '$email', '$address', '$mobile'); 插入预订(bookingID、apartmentID、clientID、date_from、date_to、nights、remarks、pax) VALUES('', $apartment, LAST_INSERT_ID(),'$booking_from', '$booking_to', '$nights', '$pax', '$remarks'); 提交;

【问题讨论】:

    标签: php mysqli insert


    【解决方案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();
    
    
    ?>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 如果表已经创建,重新创建它们是否理想?
      • @Christa 不,你没有链接只是一个例子,你可以忽略“create table”和“drop table”语句。
      • 尝试了这个建议,但由于某种原因,表格没有更新。即使我没有错误。
      • 你确定你没有错误吗?添加 echo mysqli_error($conn);在关闭连接之前。
      • 是的,我检查了“ printf("Errormessage: %s\n", $conn->error);"并没有错误。但是我想我已经找到了解决方案。我忘记提交了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2019-03-17
      相关资源
      最近更新 更多