繁体   English   中英

PHP - 编辑多个条目

[英]PHP - edit multiple entries

给定此表单,显示为表格:

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

由于两个input都有相同的名称,因此select每个input的最后一个值会覆盖以前的值。 要在具有相同名称的输入中传递多个值,请使用[]表示法:

<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迭代它们。

顺便说一句 ,在</td>之后插入<input> </td>会产生无效的 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM