带有php if块的多行Mysql insert语句

带有php if块的多行Mysql insert语句,php,mysql,Php,Mysql,我有一个可以插入多行的表单。该表单有六行字段。用户可以决定使用全部六行或使用少量字段。 当所有六行都被填满时,我使用下面的代码,它工作正常 下面的代码工作正常 //Database connection string global $conn; //insert Records $strSQLInsert = "insert into tbl_sales (Depot_Id,SalesProduct_Id,Sales_Date,Distributor_Id,Selling_Price

我有一个可以插入多行的表单。该表单有六行字段。用户可以决定使用全部六行或使用少量字段。 当所有六行都被填满时,我使用下面的代码,它工作正常

下面的代码工作正常

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

不能在双引号内写入If条件。 因此,请尝试以下方法:

$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
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product2."','".$Af_Date."','".$Af_Dist."','".$Af_Price2."','".$Af_Qty2."','".$Af_Trans."'),";
  }

$strSQLInsert .="('".$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);
试试看

//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){   
    $strSQLInsert .= "('".$Af_Depot."','".$Af_Product2."','".$Af_Date."','".$Af_Dist."','".$Af_Price2."','".$Af_Qty2."','".$Af_Trans."'),";
}

$strSQLInsert .= "('".$Af_Depot."','".$Af_Product3."','".$Af_Date."','".$Af_Dist."','".$Af_Price3."','".$Af_Qty3."','".$Af_Trans."'),";
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product4."','".$Af_Date."','".$Af_Dist."','".$Af_Price4."','".$Af_Qty4."','".$Af_Trans."'),";
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product5."','".$Af_Date."','".$Af_Dist."','".$Af_Price5."','".$Af_Qty5."','".$Af_Trans."'),";
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product6."','".$Af_Date."','".$Af_Dist."','".$Af_Price6."','".$Af_Qty6."','".$Af_Trans."'),";
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product1."','".$Af_Date."','".$Af_Dist."','".$Af_Price1."','".$Af_Qty1."','".$Af_Trans."');";

db_exec($strSQLInsert,$conn);

正如您在评论中提到的,您只是在学习PHP,所以我想指出一些事情

首先,做你的研究,了解它是什么以及如何预防它

其次,使用
$Af_PriceN
要求使用

$prices=数组(…);
$products=数组(…);
或者在理想情况下,您可以创建一个包含所有数据的世界:

类产品{
公费$id;
公帑(元);;
公共函数构造($id,$price)
{
$this->id=$id;
$this->price=$price;
}
}
$p=数组(
新产品(1,17.85),
新产品(2,19.47),
...
)
然后您可以使用来构建复杂的查询:

foreach($p作为$prod){
//将产品追加到查询
}
但回到您的示例,假设您将每个
$Af\u fooN
替换为
$Af\u foo=array
,那么您可以:

$rows=array();
对于($i=0;$i
退房并离开。 但是您仍然没有转义值,所以让我们将
db_esc
定义为类似的内容

$r=“(”.db_esc($Af_Depot)。“,”.db_esc($Af_Product[$i])。。。;
这对于打字来说真的很烦人,所以你可以利用函数的优点,比如:

//内部循环:
$pieces=阵列($Af_Depot,$Af_Product[$i],$Af_Date,$Af_Dist,
$Af_价格[$i]、$Af_数量[$i]、$Af_交易);
$pieces=数组映射('db\u esc',$pieces);
//下面是一个技巧:
$r=“(”。内爆(“,”,$pieces)。”;
所以总结一下整个代码:

//假设
$Af_仓库=。。。;
$Af_日期=。。。;
$Af_Trans=。。。;
$Af_Product=array();
$Af_Price=array();
$Af_数量=数组();
//预备
$rows=array();
//去槽项目:
对于($i=0;$i
噢,伙计,听说过循环、数组和
内爆()吗?
我还是一个初学者,还在学习php,如果你愿意演示的话,我将非常感激哇。为了使用数组,我马上做了更改,还进行了更正以防止sql注入。非常感谢你。