【问题标题】:foreach $_POST update depending on $keyforeach $_POST 更新取决于 $key
【发布时间】:2014-02-26 11:10:29
【问题描述】:

我正在尝试使用 foreach 循环更新我的数据库。每次运行循环时,都需要更改两行,但是数据库会使用相同的数据更新这两行。我的表单 php 代码是:

<input name="poles<?php echo $curtains['room_curtains_id']; ?>" type="radio" class="calc" id="<?php echo $poles['curtains_poles_id']; ?>" value="<?php echo $poles['curtains_poles_price']; ?>,<?php echo $poles['curtains_poles_id']; ?>">
<input name="pleats<?php echo $curtains['room_curtains_id']; ?>" type="radio" class="calc" id="<?php echo $pleats['curtains_pleats_id']; ?>" value="<?php echo $pleats['curtains_pleats_price']; ?>,<?php echo $pleats['curtains_pleats_id']; ?>">

发帖时,我的回报是:

Array
(
    [poles1] => 0.00,1
    [pleats1] => 40.00,2
    [poles2] => 120.00,2
    [pleats2] => 0.00,1
    [poles3] => 150.00,3
    [pleats3] => 40.00,2
)

现在在更新数据库时,我使用以下代码:

foreach ($_POST as $key => $value) {
$new_value = explode(",", $value);

if (strpos($key, 'poles')!==false) {

$result = mysql_query("UPDATE basket SET product_id = '".mysql_real_escape_string($new_value[1])."', line_price= '".mysql_real_escape_string($new_value[0])."' WHERE session_id = '$session_id' AND plot_id = '".mysql_real_escape_string($plot_id)."' AND product_type='Poles' AND rooms_curtains_id='".mysql_real_escape_string($key)."'");

}

if (strpos($key, 'pleats')!==false) {

$result2 = mysql_query("UPDATE basket SET product_id = '".mysql_real_escape_string($new_value[1])."', line_price= '".mysql_real_escape_string($new_value[0])."' WHERE session_id = '$session_id' AND plot_id = '".mysql_real_escape_string($plot_id)."' AND product_type='Pleats' AND rooms_curtains_id='".mysql_real_escape_string($key)."'");
}

}
  • 请注意,这两个查询的唯一区别是 product_type

当我运行此程序时,product_type='Poles' ($result) 的数据库已正确更新,但 product_type='Pleats' ($result2) 的信息相同,应该使用密钥 pleats1,pleats2,pleats3 进行更新。

【问题讨论】:

  • 您从未过滤过$key 变量。你告诉 MySQL:Update me the rows where product_type is 'Pleats' with the variable $key,但你从来没有指定 $key 应该包含什么
  • 您的查询接受了太多输入,最好向我们展示每列要打印的值。

标签: php mysql arrays post foreach


【解决方案1】:

您处理 MySQL 结果集的条件,但您从未处理 PHP 代码中的条件。当您指定值为Pleats 的列时,您希望MySQL 能够理解并在您的foreach() 循环中搜索%pleats%,但它永远不会发生,因为这是两件不同的事情。

mysql 查询将被执行多次,就像循环中的迭代一样。在您的情况下 - 两个查询都将执行 6 次,每个 $key

如果您想用包含pleats$key 更新包含product_type='Pleats' 的行,您应该告诉PHP。它不再是 MySQL 级别的了。

foreach ($_POST as $key => $value) {
    $new_value = explode(",", $value);

    $result = mysql_query("UPDATE basket SET product_id = '".mysql_real_escape_string($new_value[1])."', line_price= '".mysql_real_escape_string($new_value[0])."' WHERE session_id = '$session_id' AND plot_id = '".mysql_real_escape_string($plot_id)."' AND product_type='Poles' AND rooms_curtains_id='".mysql_real_escape_string($key)."'");

    if (strpos($key, 'pleats')!==false) {
        $result2 = mysql_query("UPDATE basket SET product_id = '".mysql_real_escape_string($new_value[1])."', line_price= '".mysql_real_escape_string($new_value[0])."' WHERE session_id = '$session_id' AND plot_id = '".mysql_real_escape_string($plot_id)."' AND product_type='Pleats' AND rooms_curtains_id='".mysql_real_escape_string($key)."'");
    }
}

【讨论】:

  • 谢谢,@Royal Bg,我已经用您的代码更改更新了我的问题,但现在两个结果似乎都没有运行
  • 它对我来说很好用,我只是复制了你的代码,但是用 ECHO 更改了 mysql 查询,以便打印查询,它打印查询的次数与满足条件的次数一样多。 IE。如果选中了 Pleats 的 2 个单选按钮,则只运行 2 次查询
  • 数据库要求 $key 为 int(11)。这就是查询没有更新的原因。我试过使用 $intkey = strtr($key,"poles","");但这似乎并没有从 $key 中删除文本
  • preg_match 或$intKey = strrev($key); $intKey=$intKey[0]。在较新版本的 PHP 中,您应该可以直接指定 $intKey = strrev($key)[0]$intKey = str_replace('poles', '', $key)
猜你喜欢
  • 1970-01-01
  • 2013-12-31
  • 2021-11-12
  • 2015-12-08
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多