【问题标题】:PHP - edit multiple entriesPHP - 编辑多个条目
【发布时间】:2016-09-19 03:53:04
【问题描述】:

给定这个表格,显示为表格:

<form action="multi.php" name="multi[]" method="POST" class="forms">
<table class="table-hovered">
 <tr>
  <th class="text-left highlight">attività svolta</th>
  <th class="text-left highlight">categoria</th>
 </tr>
<?php
foreach ($_POST['item'] as $k => $v)
{
    $q_item = "SELECT * FROM eventi WHERE id = '".$v."'";
    $qu_item = mysql_query($q_item);
        while($res = mysql_fetch_array($qu_item))
        {
?>
<tr>
  <td><?php echo $res['descrizione'];?></td>
  <td>
   <select name="categoria">
    <option value="<?php echo $res['categoria'];?>" selected><?php echo $res['categoria'];?>
    <option value="80"> 80
    <option value="40"> 40
    <option value="70"> 70
   </select>
  </td>
    <input type="hidden" name="idd" value="<?php echo $res['id'];?>">
 </tr>
<?php
        }
}
?>
</table>
<input type="submit" name="submit" value="modify" />
</form>

我正在尝试使用以下代码编辑多个条目:

<?php
$utente = $_SESSION["username"];
$id = $_POST["idd"];
$categoria = $_POST["categoria"];
if (!$id or !$categoria){
echo "Error";
}
else
 if ($categoria!=='80' && $categoria!=='40' && $categoria!=='70'){
 echo "Error";
}
else{
$sql="UPDATE eventi SET categoria='$categoria' WHERE id='$id'";
$update=mysql_query($sql);
echo "Entry modified correctly";
}
?>

如您所见,此代码仅更改一项。我试过让它递归。也许使用“foreach”是要走的路。

感谢任何提示。并且很抱歉使用旧版本的 PHP(我还没有切换到版本 7)。

【问题讨论】:

    标签: php foreach html-table edit


    【解决方案1】:

    由于 inputselect 的名称相同,它们中的每一个的最后一个值都会覆盖以前的值。要在具有相同名称的输入中传递多个值 - 使用 [] 表示法:

    <select name="categoria[]">
        <option value="<?php echo $res['categoria'];?>" selected><?php echo $res['categoria'];?>
        <option value="80"> 80
        <option value="40"> 40
        <option value="70"> 70
    </select>
    <input type="hidden" name="idd[]" value="<?php echo $res['id'];?>">
    

    之后 - 用 print_r 检查你的 $_POST 值 - 你会看到 $_POST[categoria]$_POST[idd]数组,您可以使用 forforeach 对其进行迭代。

    顺便说一句,在&lt;/td&gt; 之后插入&lt;input&gt; 会产生无效 html。

    【讨论】:

    • 我忘记了 [] 符号。你会使用for 还是foreach
    • 我可以试试这个:foreach ($_POST['idd'] as $k =&gt; $v) { $sql="UPDATE eventi SET categoria='$categoria' WHERE id='$id'"; $update=mysql_query($sql); }
    • 不是$categoria,而是$_POST['categoria'][$k]。与$id相同。
    • 我会测试它并发布结果。
    【解决方案2】:

    首先不需要创建任何hidden输入元素,您只需按以下方式更改&lt;select&gt;元素的name属性,

    name="categoria[<?php echo $res['id'] ?>]"
    

    所以你的代码应该是这样的,

    <form action="multi.php" name="multi[]" method="POST" class="forms">
    <table class="table-hovered">
        <tr>
            <th class="text-left highlight">attività svolta</th>
            <th class="text-left highlight">categoria</th>
        </tr>
    <?php
    foreach ($_POST['item'] as $k => $v){
        $q_item = "SELECT * FROM eventi WHERE id = '".$v."'";
        $qu_item = mysql_query($q_item);
        while($res = mysql_fetch_array($qu_item)){
    ?>
        <tr>
            <td><?php echo $res['descrizione'];?></td>
            <td>
                <select name="categoria[<?php echo $res['id'] ?>]">
                    <option value="<?php echo $res['categoria'];?>" selected><?php echo $res['categoria'];?>
                    <option value="80"> 80
                    <option value="40"> 40
                    <option value="70"> 70
                </select>
            </td>
        </tr>
    <?php
        }
    }
    ?>
    </table>
    <input type="submit" name="submit" value="modify" />
    </form>
    

    这就是你如何处理你的表单来执行UPDATE操作,

    foreach($_POST['categoria'] as $id => $categoria){
        $sql="UPDATE eventi SET categoria='". $categoria . "' WHERE id='" . $id . "'";
        // execute your query
    }
    

    注意:如果要查看完整的数组结构,请var_dump($_POST);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2021-06-15
      • 2013-05-29
      • 2021-10-04
      • 1970-01-01
      相关资源
      最近更新 更多