一个带有两个PDO查询的PHP脚本?

时间:2013-01-20 16:44:33

标签: php mysql pdo crud

我最近开始在重建客户的出租车预订系统时使用PDO。

我有一个名为create_booking.php的脚本,它最初将预订详细信息插入MySQL数据库的预订表中。插入客户详细信息后,它会检索lastinsertID以获取预订参考。然后,它在作业表中创建一个作业,并参考预订参考以关联作业/预订。

第一个插件工作正常,但第二个插件没有。有什么想法吗?

    if (isset($_POST['customer_title'])) {

  include('../assets/db_connection.php');

  $create_booking = $db->prepare("INSERT INTO bookings(customer_name, billing_address,    contact_tel, contact_mob, contact_email, party_pax, party_cases, booking_notes, price, booking_agent, booking_date, booking_status, authorised)
                                      VALUES(:customer_name, :billing_address, :contact_tel, :contact_mob, :contact_email, :party_pax, :party_cases, :booking_notes, :price, :booking_agent, :booking_date, :booking_status, :authorised );");
  $create_booking->execute(array(
      ":customer_name"       =>   $customer_title  . ' ' .   $customer_first_name  . ' '  .   $customer_last_name,
      ":billing_address"     =>   $billing_address,
      ":contact_tel"         =>   $customer_tel,
      ":contact_mob"         =>   $customer_mobile,
      ":contact_email"       =>   $customer_email,
      ":party_pax"           =>   $passengers,
      ":party_cases"         =>   $cases,
      ":booking_notes"       =>   $booking_notes,
      ":price"               =>   $price,
      ":booking_agent"       =>   $booking_agent,
      ":booking_date"        =>   $booking_date,
      ":booking_status"      =>   $booking_status,
      ":authorised"          =>   $authorised    
    ));

  $booking_ref = $db->lastInsertId('booking_ref'); // Takes Booking Ref generated in $create_booking

  $create_job    = $db->prepare("INSERT INTO jobs(booking_ref, pickup_date, pickup_time, pickup_address, destination_address, return, scheduled)
                                (:booking_ref, :pickup_date, :pickup_time, :pickup_address, :destination_address, :return, :scheduled )");

  $create_job->execute(array(

      ":booking_ref"          =>  $booking_ref,
      ":pickup_date"          =>  $pickup_date,
      ":pickup_time"          =>  $pickup_time,
      ":pickup_address"       =>  $pickup_address,
      ":destination_address"  =>  $pickup_destination,
      ":return"               =>  "N",
      ":scheduled"            =>  "N"

    ));

}

1 个答案:

答案 0 :(得分:2)

缺少第二个SQL查询VALUES

INSERT INTO() ... VALUES()

$create_job = $db->prepare("INSERT INTO jobs(booking_ref, pickup_date, pickup_time, pickup_address, destination_address, return, scheduled) VALUES (:booking_ref, :pickup_date, :pickup_time, :pickup_address, :destination_address, :return, :scheduled )");