我正在使用foreach()循环插入多行数据。但是如果它的值是税或折扣,我不想插入特定的行。我该怎么做。
我的代码:
<?php
if(isset($_POST['finalsave']))
{
foreach ($_POST['descr'] as $key => $descr)
{
$dept = mysql_real_escape_string($_POST['category']);
$idate = mysql_real_escape_string($_POST['idate']);
$desc1 = mysql_real_escape_string($descr);
$tags = mysql_real_escape_string($_POST['name'][$key]);
$location = mysql_real_escape_string($_POST['location'][$key]);
$war = mysql_real_escape_string($_POST['war'][$key]);
$qty = mysql_real_escape_string($_POST['qty'][$key]);
$rate = mysql_real_escape_string($_POST['rate'][$key]);
$finalamt = mysql_real_escape_string($_POST['finalamt'][$key]);
$sql2 = ("INSERT INTO `project`.`master_inventory` (`category`, `idate`, `descr`, `name`, `location`, `war`, `qty`, `rate`, `finalamt`)
VALUES ('".$dept."','".$idate."','".$desc1."','".$tags."','".$location."','".$war."','".$qty."','".$rate."','".$finalamt."')");
$sqlins = mysql_query($sql2);
//echo $sql2;
}
}
?>
答案 0 :(得分:2)
希望这会对你有所帮助。
foreach ($_POST['descr'] as $key => $descr)
{
if(($descr !="tax")||($descr !="discount"))
{
//Write your logic here
}
}
答案 1 :(得分:1)
将if
条件置于foreach
循环中,如下所示:
<?php
if(isset($_POST['finalsave']))
{
foreach ($_POST['descr'] as $key => $descr) {
// this you have to do
if(($descr != 'tax') || ($descr != 'discount'))
{
$dept = mysql_real_escape_string($_POST['category']);
$idate = mysql_real_escape_string($_POST['idate']);
$desc1 = mysql_real_escape_string($descr);
$tags = mysql_real_escape_string($_POST['name'][$key]);
$location = mysql_real_escape_string($_POST['location'][$key]);
$war = mysql_real_escape_string($_POST['war'][$key]);
$qty = mysql_real_escape_string($_POST['qty'][$key]);
$rate = mysql_real_escape_string($_POST['rate'][$key]);
$finalamt = mysql_real_escape_string($_POST['finalamt'][$key]);
$sql2 = ("INSERT INTO `project`.`master_inventory` (`category`, `idate`, `descr`, `name`, `location`, `war`, `qty`, `rate`, `finalamt`)
VALUES ('".$dept."','".$idate."','".$desc1."','".$tags."','".$location."','".$war."','".$qty."','".$rate."','".$finalamt."')");
$sqlins = mysql_query($sql2);
//echo $sql2;
}
}
}
?>
答案 2 :(得分:0)
在插入查询之前检查值是税还是折扣。
正如您所说,表格中的descr
字段包含分配给变量tax
的值discount
或$desc1
<?php
if(isset($_POST['finalsave']))
{
foreach ($_POST['descr'] as $key => $descr)
{
$dept = mysql_real_escape_string($_POST['category']);
$idate = mysql_real_escape_string($_POST['idate']);
//this variable $desc1 will have the value tax or discount
$desc1 = mysql_real_escape_string($descr);
$tags = mysql_real_escape_string($_POST['name'][$key]);
$location = mysql_real_escape_string($_POST['location'][$key]);
$war = mysql_real_escape_string($_POST['war'][$key]);
$qty = mysql_real_escape_string($_POST['qty'][$key]);
$rate = mysql_real_escape_string($_POST['rate'][$key]);
$finalamt = mysql_real_escape_string($_POST['finalamt'][$key]);
//inserts only if $desc1 is not tax or discount
if(($desc1!="tax")||($desc1!="discount")
{
$sql2 = ("INSERT INTO `project`.`master_inventory` (`category`, `idate`, `descr`, `name`, `location`, `war`, `qty`, `rate`, `finalamt`)
VALUES ('".$dept."','".$idate."','".$desc1."','".$tags."','".$location."','".$war."','".$qty."','".$rate."','".$finalamt."')");
$sqlins = mysql_query($sql2);
}
}
}
?>