2016-09-18 45 views
1

鑑於這種形式,顯示爲表: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)。

回答

1

沒有必要創建擺在首位任何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

2

由於您有相同的名稱爲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