我有一個表格插入多行。這表格有六行fields.a用戶可以決定使用所有六行或使用幾個。 當所有六行填滿我使用下面的代碼,這工作正常。下面多行Mysql插入語句與PHP如果塊
的代碼工作正常
//Database connection string
global $conn;
//insert Records
$strSQLInsert = "insert into tbl_sales (Depot_Id,SalesProduct_Id,Sales_Date,Distributor_Id,Selling_Price,Units_Sold,Trns_type) values
('".$Af_Depot."','".$Af_Product2."','".$Af_Date."','".$Af_Dist."','".$Af_Price2."','".$Af_Qty2."','".$Af_Trans."'),
('".$Af_Depot."','".$Af_Product3."','".$Af_Date."','".$Af_Dist."','".$Af_Price3."','".$Af_Qty3."','".$Af_Trans."'),
('".$Af_Depot."','".$Af_Product4."','".$Af_Date."','".$Af_Dist."','".$Af_Price4."','".$Af_Qty4."','".$Af_Trans."'),
('".$Af_Depot."','".$Af_Product5."','".$Af_Date."','".$Af_Dist."','".$Af_Price5."','".$Af_Qty5."','".$Af_Trans."'),
('".$Af_Depot."','".$Af_Product6."','".$Af_Date."','".$Af_Dist."','".$Af_Price6."','".$Af_Qty6."','".$Af_Trans."'),
('".$Af_Depot."','".$Af_Product1."','".$Af_Date."','".$Af_Dist."','".$Af_Price1."','".$Af_Qty1."','".$Af_Trans."')";
db_exec($strSQLInsert,$conn);
但是當我嘗試包括PHP的if語句來離開了空行它給我一個錯誤。 在下面的代碼中,我已經嘗試過測試第一行,但它不工作。如果您有任何想法,請協助。
//Database connection string
global $conn;
//insert Records
$strSQLInsert = "insert into tbl_sales (Depot_Id,SalesProduct_Id,Sales_Date,Distributor_Id,Selling_Price,Units_Sold,Trns_type) values
"if ($Af_Qty2 > 0){ //error occurs here
('".$Af_Depot."','".$Af_Product2."','".$Af_Date."','".$Af_Dist."','".$Af_Price2."','".$Af_Qty2."','".$Af_Trans."'),
}"
('".$Af_Depot."','".$Af_Product3."','".$Af_Date."','".$Af_Dist."','".$Af_Price3."','".$Af_Qty3."','".$Af_Trans."'),
('".$Af_Depot."','".$Af_Product4."','".$Af_Date."','".$Af_Dist."','".$Af_Price4."','".$Af_Qty4."','".$Af_Trans."'),
('".$Af_Depot."','".$Af_Product5."','".$Af_Date."','".$Af_Dist."','".$Af_Price5."','".$Af_Qty5."','".$Af_Trans."'),
('".$Af_Depot."','".$Af_Product6."','".$Af_Date."','".$Af_Dist."','".$Af_Price6."','".$Af_Qty6."','".$Af_Trans."'),
('".$Af_Depot."','".$Af_Product1."','".$Af_Date."','".$Af_Dist."','".$Af_Price1."','".$Af_Qty1."','".$Af_Trans."')";
db_exec($strSQLInsert,$conn);
哦,聽說過循環,數組和'implode()'? – Vyktor 2013-04-10 07:48:38
我還是一個初學者,還在學習php,如果你在意示範,我會成爲gratefull – Law 2013-04-10 08:16:35