鑑於這種形式,顯示爲表: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)。
您的解決方案工作。 – Alex