0

私は以前にこの質問をしましたが、それ以来コードを変更しました。フォームデータをテーブルに挿入するこのスクリプトに問題があります。最初の挿入は、顧客の連絡先の詳細を格納する予約を作成します。2番目の挿入は、最初に作成された予約参照を取得し、顧客の「JOB」を作成します。最後の挿入は、顧客の帰りの旅である2番目の「JOB」を作成することになっています。

最初の2つの挿入は正常に実行されていますが、最後の挿入である2番目のJOB挿入は無視されました。

テーブルの構造を確認しました。データはスクリプトに渡されましたが、すべて問題ないので、問題はスクリプト(以下に表示)にある必要があります。助けていただければ幸いです。

1つのスクリプトを使用して同じテーブルに2回挿入するのは正しいですか?

<?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
  ));




}


?>
4

3 に答える 3

1

同じデータを 2 回挿入することは、最も重要なデータベース アーキテクチャの法則の 1 つであるデータベースの正規化の原則に違反するため、これは間違いです。

ただし、技術的な問題はありません。mysql からのエラー メッセージを使用してキャッチする必要があるいくつかの間違いがあります。これを行うには、PDO に接続した後にこの行を追加します。

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

実際のエラーをキャッチすることが、SQL クエリをデバッグする唯一の方法であることに注意してください。コードを見るだけでは意味がありません。

return は mysql キーワードでなければなりません。次のように書きます

`return`

ところで、私はそのような途方もなく巨大なコードに耐えられません。

私があなたなら、50 行ではなく 10 行で書きます。

$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);
于 2013-02-05T15:52:13.703 に答える
0

booking_refテーブルの主キーのように見えjobsます。同じキーを 2 回挿入しようとすると、最終的なクエリが失敗します。

jobs自動インクリメント番号だけである主キーである別のフィールドが必要であり、次にインデックスを作成しますbooking_ref

于 2013-02-05T15:47:47.607 に答える
-1

それを禁止する法律はありません。必要なことは、最後の INSERT クエリの戻り値を確認することです。私の最善の推測はjobs、二重挿入で違反しているテーブルに一意のインデックスがあることです。

ここで mySQLi を使用しているのか PDO を使用しているのかは明らかではありませんが、どちらの実行関数もfalse失敗すると戻ります。そのため、それをキャッチしてから、それぞれのオブジェクトのエラー関数を呼び出して、何が問題なのかを取得する必要があります。

于 2013-02-05T15:48:05.603 に答える