PHP - 编辑多个条目

PHP - edit multiple entries

鉴于此表格,显示为 table:

<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)。

由于 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.

迭代它们

Btw,在 </td> 之后插入一个 <input> 会产生 invalid html.

首先不需要创建任何 hidden 输入元素,您只需按以下方式更改 <select> 元素的 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);