0

I have a form where the user can insert up to five line items for an invoice. The easiest way for me to do this is to just do five inserts and do a isset check before each query. However, the problem is if I try to run the two queries one after another only the first one inserts the data. I know I can combine them into one PDO query (and that does in fact work), but it does not suit my needs. The second query does not insert.

// Connect to the database
$conn = new PDO("mysql:host=$DB_HOST;dbname=$DB_DATABASE",$DB_USER,$DB_PASSWORD);

//Set all the data here 
$receiptid = $_POST['receiptid'];
    // .. the rest of the POST data gets set here.


//Insert first line item
$sql = "INSERT INTO lineitems (receiptid, service, description, quantity, unitprice, linetotal) 
        VALUES (:receiptid, :service, :description, :quantity, :unitprice, :linetotal)";
$q = $conn->prepare($sql);
$q->execute(array(':receiptid'=>$receiptid,
                    ':service'=>$service,
                    ':description'=>$description,
                    ':quantity'=>$quantity,
                    ':unitprice'=>$unitprice,
                    ':linetotal'=>$linetotal));

//Insert second line item
$sql = "INSERT INTO lineitems (receiptid, service2, description2, quantity2, unitprice2, linetotal2) 
        VALUES (:receiptid, :service2, :description2, :quantity2, :unitprice2, :linetotal2)";
$q = $conn->prepare($sql);
$q->execute(array(':receiptid'=>$receiptid,
                    ':service2'=>$service2,
                    ':description2'=>$description2,
                    ':quantity2'=>$quantity2,
                    ':unitprice2'=>$unitprice2,
                    ':linetotal2'=>$linetotal2));
4

1 に答える 1

1

あなたのテーブルには、入力された項目番号 (サービス 2、説明 2 など) ごとに異なる列が本当にありますか?

おそらく、2 番目の挿入のフィールド名を最初の挿入のフィールド名と一致するように変更する必要があります。

期待どおりのクエリ結果が得られなかった場合 (つまり、実行結果を確認し、何かが失敗した場合にエラーを確認するなど) を処理していれば、問題の原因を急いで突き止めることができます。

于 2012-09-17T15:02:16.733 に答える