2

複数の行を挿入するフォームがあります。フォームには 6 行のフィールドがあります。ユーザーは、6 行すべてを使用するか、いくつかを使用するかを決定できます。6 行すべてが入力されたら、以下のコードを使用します。これは問題なく動作します。

以下のコードは正常に動作します

    //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);
4

3 に答える 3