Alex Alex - 2 months ago 16
PHP Question

PHP - edit multiple entries

Given this form, shown as a 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>


I am trying to edit multiple entries, using the code below:

<?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";
}
?>


As you see, this code changes just one item. I have tried making it recursive. Maybe using a "foreach" is the way to go.

Any hints are appreciated. And sorry for using an old version of PHP (I haven't switched to version 7 yet).

Answer

There's no need to create any hidden input element in the first place, you just have to change the name attribute of <select> element in the following way,

name="categoria[<?php echo $res['id'] ?>]"

So your code should be like this,

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

And this is how you can process your form to perform UPDATE operation,

foreach($_POST['categoria'] as $id => $categoria){
    $sql="UPDATE eventi SET categoria='". $categoria . "' WHERE id='" . $id . "'";
    // execute your query
}

Note: If you want to see the complete array structure, do var_dump($_POST);

Comments