2016-04-28 12 views
0

PHPを使用してSQLite3 DBに自動的に生成される乱数の重複エントリを避けようとしています。そのために、do whileループでステートメントを準備しました。乱数が生成され、その数が既に存在するかどうか照会が検査されます。はいの場合は、生成します。Prepared Statement(PHP5、SQLite3)が30秒を超える実行時間Time

Atleastのは、これは私が達成しようとしています何です...

しかし、私には未知の何らかの理由で、PHPのログは30秒の最大実行時間をクエリ行でexeededされたことを私に示し続けて

。まず、私は準備された声明なしですべてをやってみたが、うまくいかなかった。私はそれが質問にPHP変数を持っていたからだと思った。だから私はPrepared Statementsに成功せずに切り替えました。

すべてのPOST変数をFirebug経由でチェックしましたが、すべてが正常であるようです。それは私に下痢を与えている準備された声明です!

あなたは私を助けてくれますか?

PHPコード:で始まる

<?php 

$adate = $_POST['adate']; 
$ddate = $_POST['ddate']; 
$ad  = $_POST['ad']; 
$dd  = $_POST['dd']; 
$fname = $_POST['fname']; 
$lname = $_POST['lname']; 
$email = $_POST['email']; 
$address = $_POST['address']; 
$postal = $_POST['postal']; 
$city  = $_POST['city']; 
$country = $_POST['country']; 
$tel  = $_POST['tel']; 
$message = $_POST['message']; 
$price = $_POST['price']; 
$bkfst = $_POST['bkfst']; 
$rnum  = $_POST['rnum']; 
$rtype = $_POST['rtype']; 
$robotest = $_POST['blnk']; 
$bid  = 0; 
$cid  = 0; 
$adate = $adate . " 20:00:00"; 
$ddate = $ddate . " 13:00:00"; 

if ($robotest) 
    $error = "You are a gutless robot."; 

else { 

    function bid() 
    { 
     $bid = mt_rand(111111, 999999); 
     if (($bid % 10) == 0) { 
      $bid = $bid + 123; 
     } 
    } 

    function cid() 
    { 
     $cid = mt_rand(11111, 99999); 
     if (($cid % 10) == 0) { 
      $cid = $cid + 123; 
     } 
    } 

    include 'connect.php'; 

    do { 
     cid(); 
    --> $sth = $db->prepare("SELECT COUNT (CustomerID) from Customer WHERE CustomerID = ?"); 
     $sth->execute(array($cid)); 
    } while ($sth->fetchColumn() > 0); 

    $sth = $db->prepare("INSERT INTO Customer (CustomerID, FirstName, LastName, Address, PostalCode, City, Country, EMail, Phone) VALUES ('$cid', '$fname', '$lname', '$address', '$postal', '$city', '$country', '$email', '$tel')"); 
    $sth->execute(); 

    do { 
     bid(); 
    --> $sth = $db->prepare("SELECT COUNT (BookingID) from Booking WHERE BookingID = ?"); 
     $sth->execute(array($bid)); 
    } while ($sth->fetchColumn() > 0); 

    $sth = $db->prepare("INSERT INTO Booking (BookingID, Arrival, Checkout, RoomNumber, CustomerID, Breakfast, Comment, Paid) VALUES ('$bid', '$adate', '$ddate', '$rnum', '$cid', '$bkfst', '$message', 'N')"); 
    $sth->execute(); 

    $subject = "Your Booking"; 
    $message = "Hi $fname,\n\nA $rtype from $ad to $dd has been booked for you.\n\nYour Booking Code is $bid.\n\nRegards."; 

    mail($email, $subject, $message); 

    echo 'The Booking completed successfully! Check your E-Mail for further Information.'; 
} 

?> 

ライン - コードでは>問題の行です。

そしてはい、私がやっても、スタックオーバーフローのフォーラムに迷惑な人々によって学習によって学習された初心者です:)

感謝。

編集:

これは私のコードが今見える方法です。すべてのエラーはなくなりましたが、PHPはDBに何も挿入していません。生成された番号で電子メールが正しく送信されます。

<?php 

$adate = $_POST['adate']; 
$ddate = $_POST['ddate']; 
$ad  = $_POST['ad']; 
$dd  = $_POST['dd']; 
$fname = $_POST['fname']; 
$lname = $_POST['lname']; 
$email = $_POST['email']; 
$address = $_POST['address']; 
$postal = $_POST['postal']; 
$city  = $_POST['city']; 
$country = $_POST['country']; 
$tel  = $_POST['tel']; 
$message = $_POST['message']; 
$price = $_POST['price']; 
$bkfst = $_POST['bkfst']; 
$rnum  = $_POST['rnum']; 
$rtype = $_POST['rtype']; 
$robotest = $_POST['blnk']; 
$adate = $adate . " 20:00:00"; 
$ddate = $ddate . " 13:00:00"; 
$cid; 
$bid; 

if ($robotest) 
    $error = "You are a gutless robot."; 

else { 

    function bid() 
    { 
     global $bid; 
     $bid = mt_rand(111111, 999999); 
     if (($bid % 10) == 0) { 
      $bid = $bid + 123; 

     } 
    } 

    function cid() 
    { 
     global $cid; 
     $cid = mt_rand(11111, 99999); 
     if (($cid % 10) == 0) { 
      $cid = $cid + 123; 

     } 
    } 

    include 'connect.php'; 

    do { 
     global $cid; 
     cid(); 
     $sth = $db->prepare('SELECT COUNT (CustomerID) from Customer WHERE CustomerID = ?'); 
     $sth->execute(array($cid)); 
     } while ($sth->fetchColumn() > 0); 


    global $cid; 
    $sth = $db->prepare('INSERT INTO Customer (CustomerID, FirstName, LastName, Address, PostalCode, City, Country, EMail, Phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'); 
    $sth->execute(array($cid, $fname, $lname, $address, $postal, $city, $country, $email, $tel)); 

    do { 
     global $bid; 
     bid(); 
     $sth = $db->prepare('SELECT COUNT (BookingID) from Booking WHERE BookingID = ?'); 
     } while ($sth->fetchColumn() > 0); 


    global $bid; 
    global $cid; 
    $sth = $db->prepare('INSERT INTO Booking (BookingID, Arrival, Checkout, RoomNumber, CustomerID, Breakfast, Comment, Paid) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'); 
    $sth->execute(array($bid, $adate, $ddate, $rnum, $cid, $bkfst, $message, 'N')); 


    $subject = "Your Booking"; 
    global $bid; 
    $message = "Hi $fname,\n\nA $rtype from $ad to $dd has been booked for you.\n\nYour Booking Code is $bid.\n\nRegards."; 
    mail($email, $subject, $message); 


    echo 'The Booking completed successfully! Check your E-Mail for further Information.'; 
} 

?> 

hhmmm ...

+0

なぜ固有の乱数が必要ですか? – Mike

+1

なぜあなたはどこにでもパラメータを設定していませんか? – chris85

+1

変数スコープについて学ぶ必要があります。関数内の '$ cid()'と '$ bid 'は、前に定義したcid/bigではありません。関数が復帰するときに "内部"変数が破壊されるので、関数は本質的に役に立たない。あなたは 'global $ bid'を持っていなければならない、あるいは新しい値を返す必要があり、それらの値をどこでも必要とするところで' $ cid = cid() 'を持つ必要があります。 –

答えて

2

これは無限ループです:あなたのCID /入札()関数はひどく構築されているので(、あなたはこのDOの内側に使用している$cid

do { 
    cid(); 
    $sth = $db->prepare("SELECT COUNT (CustomerID) from Customer WHERE CustomerID = ?"); 
    $sth->execute(array($cid)); 
} while ($sth->fetchColumn() > 0); 

)ループNEVER$cid = 0からスクリプトの先頭に変更されます。

ループが開始されると、CustomerID = 0でクエリを準備/実行し、フェッチしたcount()結果でデータの行の1つを取得します。

周りに再びループロール、およびクエリを再実行し、正確に同じ$ CID = 0値を持つので、あなたは、ループの終了条件をリセットし続ける - あなたは値で終わることはありません、クエリを保つため、不良/無効cid = 0と同じである。

古き良きBASICプログラムのようにほぼ同じです:10 GOTO 10

+0

私は最初に '$ cid = 0;'を実行することで、グローバルに宣言しました。関数内でグローバルな '$ cid'を使うにはどうしたらいいですか? – Zombievirus

+0

@Zombievirus 'cid()'関数の中に 'global $ cid;'を入れ、 'bid()'関数の中に 'global $ bid;'を入れる必要があります。 – Barmar

+0

ええと、私が考え出したことは、超過したエラーはなくなりましたが、今はinsert文でグローバル変数を使用する方法を理解しようとしています... – Zombievirus

0

今は働いていないです:

<?php 

$adate = $_POST['adate']; 
$ddate = $_POST['ddate']; 
$ad  = $_POST['ad']; 
$dd  = $_POST['dd']; 
$fname = $_POST['fname']; 
$lname = $_POST['lname']; 
$email = $_POST['email']; 
$address = $_POST['address']; 
$postal = $_POST['postal']; 
$city  = $_POST['city']; 
$country = $_POST['country']; 
$tel  = $_POST['tel']; 
$message = $_POST['message']; 
$price = $_POST['price']; 
$bkfst = $_POST['bkfst']; 
$rnum  = $_POST['rnum']; 
$rtype = $_POST['rtype']; 
$robotest = $_POST['blnk']; 
$adate = $adate . " 20:00:00"; 
$ddate = $ddate . " 13:00:00"; 
$cid; 
$bid; 

if ($robotest) 
    $error = "You are a gutless robot."; 

else { 

    function bid() 
    { 
     global $bid; 
     $bid = mt_rand(111111, 999999); 
     if (($bid % 10) == 0) { 
      $bid = $bid + 123; 

     } 
    } 

    function cid() 
    { 
     global $cid; 
     $cid = mt_rand(11111, 99999); 
     if (($cid % 10) == 0) { 
      $cid = $cid + 123; 

     } 
    } 

    include 'connect.php'; 

    $sth = $db->prepare('SELECT COUNT (EMail) from Customer WHERE EMail = ?'); 
    $sth->execute(array($email)); 
    if($sth->fetchColumn() < 1){ 

    do { 
     global $cid; 
     cid(); 
     $sth = $db->prepare('SELECT COUNT (CustomerID) from Customer WHERE CustomerID = ?'); 
     $sth->execute(array($cid)); 
     } while ($sth->fetchColumn() > 0); 

    global $cid; 
    $sth = $db->prepare('INSERT INTO Customer (CustomerID, FirstName, LastName, Address, PostalCode, City, Country, EMail, Phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'); 
    $sth->execute(array($cid, $fname, $lname, $address, $postal, $city, $country, $email, $tel)); 

    }else{ 
    global $cid;  
    $sth = $db->prepare('SELECT CustomerID from Customer WHERE EMail = ?'); 
    $sth->execute(array($email)); 
    $id = $sth->fetch(PDO::FETCH_ASSOC); 
    $cid = $id['CustomerID']; 
    } 

    do { 
     global $bid; 
     bid(); 
     $sth = $db->prepare('SELECT COUNT (BookingID) from Booking WHERE BookingID = ?'); 
     } while ($sth->fetchColumn() > 0); 


    global $bid; 
    global $cid; 
    $booktime = date('Y-m-d H:i:s'); 
    $sth = $db->prepare('INSERT INTO Booking (BookingID, Arrival, Checkout, RoomNumber, CustomerID, Breakfast, Comment, Paid, BookTime, Invoice) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); 
    $sth->execute(array($bid, $adate, $ddate, $rnum, $cid, $bkfst, $message, 'N', $booktime, NULL)); 


    $subject = "Your Booking"; 
    global $bid; 
    $message = "Hi $fname,\n\nA $rtype from $ad to $dd has been booked for you.\n\nYour Booking Code is $bid.\n\nMention this Code if you need to get in touch with us.\n\nRegards."; 
    mail($email, $subject, $message); 


    echo 'The Booking completed successfully! Check your E-Mail for further Information.'; 
} 

?> 

全く分から、これはそれを行うための最善の方法ですが、それは完璧に動作していますか。

すべてのヒントをありがとう。

関連する問題