0

我有一个表单,用户可以在其中插入最多五个行项目的发票。对我来说,最简单的方法是只进行五次插入并isset在每次查询之前进行检查。但是,问题是如果我尝试一个接一个地运行这两个查询,那么只有第一个会插入数据。我知道我可以将它们组合成一个 PDO 查询(这确实有效),但它不适合我的需要。第二个查询不插入。

// 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

对于每个输入的项目编号(即 service2、descriptions2 等),您的表是否真的有不同的列?

也许您需要更改第二次插入中的字段名称以匹配第一次中的字段名称。

If you were handling cases where you did not get expected query result properly (i.e. checking your execution results and looking at the errors if something fails, You would be able to get to the source of the problem in a hurry.)

于 2012-09-17T15:02:16.733 回答