0

正如您在这段代码中看到的那样,我通过从数据库中的表中选择模型来创建一个表。但是我发布了选择查询的返回,就像该表的主列并将其放入 while 循环中,以便它不断生成行直到选择查询附带的模型完成现在我遇到了一个问题,当我试图在 $_Post[''] supergloble 中获取这些模型时,它只会向我发送它从循环中获得的最后一个值我的问题是如何从这个循环中获取每个值以在我的数据库中的单个插入查询中使用它?

抱歉英语不好 :S !!

<form class="form-signin" action="<?php $_SERVER['PHP_SELF'];?>" method="Post">
<?php
$models = mysql_query("SELECT `Model_Name` FROM `models` WHERE `Brand` = 20");
while($row = mysql_fetch_array($models))
    {
    echo '
    <tr>
    <td><input type="text" name="mode[]" value="'.$row['Model_Name'].'"></td>
    <td><input type="text" name="sellout[]" value=""></td>
    <td><input type="text" name="shelfshare[]" value=""></td>
    <td><input type="text" name="price[]" value=""></td>
    <td><input type="text" name="Shortage[]" value=""></td>
    <td><input type="text" name="Inventory[]" value=""></td>
     </tr>

    ';
    }

    ?>

</form>

插入脚本

$date = date("Y-m-d");
foreach($_POST['mode'] as $key => $mode){
$sellout = $_POST['sellout'][$key];
$shelfshare = $_POST['shelfshare'][$key];
$price = $_POST['price'][$key];
$shortage = $_POST['shortage'][$key];
$inventory = $_POST['inventory'][$key];
mysql_query("INSERT INTO `smartdailyreport`(`SFO_Code`, `Model`, `Sell_Out`, `Shelf_Share`, `Price`, `Shortage`, `Inventory`, `Date`) VALUES ('".mysql_real_escape_string($_SESSION['idd'])."','".mysql_real_escape_string($mode)."','".mysql_real_escape_string($sellout)."','".mysql_real_escape_string($shelfshare)."','".mysql_real_escape_string($price)."','".mysql_real_escape_string($shortage)."','".mysql_real_escape_string($inventory)."','".mysql_real_escape_string($date)."')") or die(mysql_error());
}
4

2 回答 2

1

将这些输入的名称设为数组:

 <tr>
<td><div class="col3" align="center"><input type="text" name="mode[]" class="form-control" value="'.$row['Model_Name'].'"></div></td>
<td><div class="col3" align="center"><input type="text" name="sellout[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="shelfshare[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="price[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="Shortage[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="Inventory[]" class="form-control" value=""></div></td>
 </tr>

然后当您处理表单时:

foreach($_POST['mode'] as $key=>$mode){
    $thisIsOne = $_POST['mode'][$key];
    $alsoThisOne = $_POST['sellout'][$key];
    etc...
}
于 2013-10-29T01:06:22.880 回答
0

我会把我的评论变成你的答案......

你有$POST你的$mode价值,修复它。(或删除它,$mode已经从 中定义foreach

将查询放入foreach循环中,否则每次迭代时只需覆盖这些变量,然后在末尾插入最后一个

如果有一个错误(如果你愿意),则放入回调中以向mysql_error您显示die错误mysql_query

$date = date("Y-m-d");
foreach($_POST['mode'] as $key => $mode){
    $sellout = $_POST['sellout'][$key];
    $shelfshare = $_POST['shelfshare'][$key];
    $price = $_POST['price'][$key];
    $shortage = $_POST['shortage'][$key];
    $inventory = $_POST['inventory'][$key];
    mysql_query("INSERT INTO `smartdailyreport`(`SFO_Code`, `Model`, `Sell_Out`, `Shelf_Share`, `Price`, `Shortage`, `Inventory`, `Date`) VALUES ('".mysql_real_escape_string($_SESSION['idd'])."','".mysql_real_escape_string($mode)."','".mysql_real_escape_string($sellout)."','".mysql_real_escape_string($shelfshare)."','".mysql_real_escape_string($price)."','".mysql_real_escape_string($shortage)."','".mysql_real_escape_string($inventory)."','".mysql_real_escape_string($date)."')") or die(mysql_error());
}

最后,使用mysqli_*而不是mysqlasmysql已经被弃用了一段时间。而且,使用mysqli_real_escape_string或类似方法来转义您的 POST 变量并让您免于 SQL 注入

于 2013-10-29T02:17:03.737 回答