2013-02-05 63 views
0

我以前问过这个问题,但自从改变了我的代码。我在将表单数据插入表格的脚本时遇到了麻烦。第一次插入创建一个预订,存储客户的联系信息。第二次插入采用第一次创建的预订编号并为客户创建一个“工作”。最终的插页应该创建第二个'工作',客户的回程。在同一个脚本中两次插入一个表?

前两个插入正常运行, 但它忽略了最后一个,第二个JOB插入。

我检查了表结构,并且数据已传递给脚本一切正常,所以问题必须在脚本(如下所示)任何帮助,非常感谢。

使用一个脚本两次插入同一个表是否正确?

<?php 

    $customer_title   = $_POST['customer_title']; 
    $customer_first_name  = $_POST['customer_first_name']; 
    $customer_last_name  = $_POST['customer_last_name']; 
    $billing_address   = $_POST['billing_address']; 
    $customer_tel    = $_POST['customer_tel']; 
    $customer_mobile   = $_POST['customer_mobile']; 
    $customer_email   = $_POST['customer_email']; 
    $passengers    = $_POST['passengers']; 
    $cases     = $_POST['cases']; 
    $return_flight_number  = $_POST['return_flight_number']; 
    $price     = $_POST['price']; 
    $pickup_date    = $_POST['pickup_date']; 
    $pickup_time    = $_POST['pickup_time']; 
    $pickup_address   = $_POST['pickup_address']; 
    $destination_address  = $_POST['pickup_destination']; 
    $return_date    = $_POST['return_date']; 
    $return_time    = $_POST['return_time']; 
    $return_pickup   = $_POST['return_pickup']; 
    $return_destination  = $_POST['return_destination']; 
    $booking_notes   = $_POST['booking_notes']; 

    $booking_status   = "Confirmed"; 
    $authorised    = "N"; 

    $booking_agent   = "ROOT_TEST"; 
    $booking_date    = date("Y/m/d"); 

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 

    $scheduled = "N"; 

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

    $create_job->execute(array(
     ":booking_ref"   => $booking_ref, 
     ":pickup_date"   => $pickup_date, 
     ":pickup_time"   => $pickup_time, 
     ":pickup_address"  => $pickup_address, 
     ":destination_address" => $destination_address, 
     ":scheduled"   => $scheduled 
)); 



    $return = "Y"; 


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

    $create_return->execute(array(
     ":booking_ref"   => $booking_ref, 
     ":pickup_date"   => $return_date, 
     ":pickup_time"   => $return_time, 
     ":pickup_address"  => $return_pickup, 
     ":destination_address" => $return_destination, 
     ":scheduled"   => $scheduled, 
     ":return"    => $return 
)); 




} 


?> 
+0

可以vardump后值执行第三查询之前,看看他们是否持有正确的数据? – GGio

+0

您是否已将'PDO :: ATTR_ERRMODE'设置为'PDO :: ERRMODE_WARNING'(启用了E_WARNING)或PDO :: ERRMODE_EXCEPTION? –

回答

1

这是不正确的肯定,作为插入相同的数据两次违反了最重要的数据库架构定律之一 - Database Normalization原则

但是,它没有任何技术问题。有一些错误,您必须使用mysql中的错误消息来捕获。要获得它,请在连接到PDO后添加此行。

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

请注意,捕获实际错误是调试SQL查询的唯一方法。只是看代码没有意义也没有帮助。

return必须是一个mysql关键字。把它写成

`return` 

顺便说一下,我受不了如此巨大的代码。

如果我是你,我会让它在10日线,不是50:

$allowed = array('customer_name', 'billing_address', 'contact_tel', 'contact_mob', 
       'contact_email', 'party_pax', 'party_cases', 'booking_notes', 'price'); 
$insert = $db->filterArray($_POST,$allowed); 

$insert['booking_status'] = "Confirmed"; 
$insert['authorised']  = "N"; 
$insert['booking_agent'] = "ROOT_TEST"; 
$insert['booking_date'] = date("Y-m-d"); 

$db->query("INSERT INTO bookings SET ?u", $insert); 
+0

谢谢,你是对的,它是没有任何一方不喜欢的返回字段名称。我也会按照你的建议减少我的代码。再次感谢 :) –

0

看起来booking_ref是在jobs表的主键,您试图插入相同的键两次这就是为什么最终查询失败。

您应该有一个单独的字段,它是jobs上的主键,它只是一个自动递增的数字,然后在booking_ref上创建一个索引。

+0

job_ref上有一个主键,我检查过phpmyadmin,唯一的主键是job_ref,而且booking_ref没有设置为唯一。 –

-1

有没有法律反对它。你需要做的是检查最后一个INSERT查询的返回值。我最好的猜测是jobs表中有一个唯一索引,您违反了双插入操作。

如果你在这里使用mySQLi或PDO并不明显,但是两个执行函数在失败时都会返回false,所以你应该捕获它,然后调用相应对象的错误函数来得到错误。

相关问题