SQL错误,我试图在3表中插入数据,因为其他2是桥

时间:2017-03-08 17:35:51

标签: php sql mariadb

错误:

  

您的SQL语法有错误;检查与您的MariaDB服务器版本对应的手册,以便在第1行的')'附近使用正确的语法

还有更好的方法吗?

$insertSQL = sprintf("INSERT INTO booking (book_id,book_start_rent,book_end_rent,book_total,staff_id) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['book_id'], "text"),
                       GetSQLValueString($_POST['book_start_rent'], "date"),
                       GetSQLValueString($_POST['book_end_rent'], "date"),
                       GetSQLValueString($_POST['book_total'], "double"),
                       GetSQLValueString($_POST["state2"], "text")

                       /"INSERT INTO car_booking (car_id, book_id) VALUES (%s, %s)",
                                            GetSQLValueString($_POST['state'], "text"),
                                            GetSQLValueString($_POST['book_id'], "text")

                          /"INSERT INTO member_booking (mem_id, book_id) VALUES (%s, %s)",
                              GetSQLValueString($colname_information2, "text"),
                              GetSQLValueString($_POST['book_id'], "text")

                            );

编辑:我试着加分号;在插入结束时,出现错误syntax error, unexpected ';'

$insertSQL = sprintf("INSERT INTO booking (book_id,book_start_rent,book_end_rent,book_total,staff_id) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['book_id'], "text"),
                       GetSQLValueString($_POST['book_start_rent'], "text"),
                       GetSQLValueString($_POST['book_end_rent'], "text"),
                       GetSQLValueString($_POST['book_total'], "double"),
                       GetSQLValueString($_POST["state2"], "text");

                       "INSERT INTO car_booking (car_id, book_id) VALUES (%s, %s)",
                                            GetSQLValueString($_POST['state'], "text"),
                                            GetSQLValueString($_POST['book_id'], "text");

                          "INSERT INTO member_booking (mem_id, book_id) VALUES (%s, %s)",
                              GetSQLValueString($colname_information2, "text"),
                              GetSQLValueString($_POST['book_id'], "text");

                            );

编辑2:我终于可以插入,但是有新错误

  

无法添加或更新子行:外键约束失败(car rental bookingmember_booking,CONSTRAINT FK__member FOREIGN KEY   (mem_id)参考membermem_id)ON更新级联更新   CASCADE)

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
                              $insertSQL = sprintf("INSERT INTO booking (book_id,book_start_rent,book_end_rent,book_total,staff_id) VALUES (%s, %s, %s, %s, %s)",
                                                   GetSQLValueString($_POST['book_id'], "text"),
                                                   GetSQLValueString($_POST['book_start_rent'], "text"),
                                                   GetSQLValueString($_POST['book_end_rent'], "text"),
                                                   GetSQLValueString($_POST['book_total'], "double"),
                                                   GetSQLValueString($_POST["state2"], "text"));

                $insertSQL2 = sprintf("INSERT INTO car_booking (car_id, book_id) VALUES (%s, %s)",
                                     GetSQLValueString($_POST['state'], "text"),
                                     GetSQLValueString($_POST['book_id'], "text")  );

                                     $insertSQL3 = sprintf("INSERT INTO member_booking (mem_id, book_id) VALUES (%s, %s)",
                                         GetSQLValueString($colname_information2, "text"),
                                         GetSQLValueString($_POST['book_id'], "text")  );

1 个答案:

答案 0 :(得分:0)

您可以使用PDO:

// init PDO connection
$db = new PDO('mysql:host=localhost;dbname=testdb;', 'username', 'password');
$sql1 = "INSERT INTO booking (book_id, book_start_rent, book_end_rent, book_total, staff_id) VALUES (:book_id, :book_start, :book_end_rent, :book_total, :staff_id)";
$prepared1 = $db->prepare($sql);
$result1 = $prepared->execute(
    array(
        ':book_id' => GetSQLValueString($_POST['book_id'], "text"),
        ':book_start' => GetSQLValueString($_POST['book_start_rent'], "date"),
        ':book_end_rent' => GetSQLValueString($_POST['book_end_rent'], "date"),
        ':book_total' => GetSQLValueString($_POST['book_total'], "double"),
        ':staff_id' => GetSQLValueString($_POST["state2"], "text"),
    )
);
// and so on for each of your 3 queries