PHP - 编辑多个条目

PHP - 编辑多个条目

问题描述:

鉴于这种形式,显示为表:PHP - 编辑多个条目

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

没有必要创建摆在首位任何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);

+0

您的解决方案工作。 – Alex

由于您有相同的名称为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

顺便说一句,插入</td>产生无效 HTML一个后<input>权。

+0

我忘了[]符号。你会使用'for'还是'foreach'? – Alex

+0

没关系,两者都可以。 –

+0

我可能会尝试这样做:'foreach($ _POST ['idd']为$ k => $ v) { $ sql =“UPDATE eventi SET categoria ='$ categoria'WHERE id ='$ id'”; $ update = mysql_query($ sql); }' – Alex