1

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

0

2 Answers 2

2

As you have same names for both input and select the last value of each of them overwrites previous values. For passing multiple values in inputs with same names - use [] notation:

<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'];?>">

After that - check your $_POST values with print_r - you will see that $_POST[categoria] and $_POST[idd] are arrays and you can iterate over them with for or foreach.

Btw, inserting an <input> right after </td> produces invalid html.

Sign up to request clarification or add additional context in comments.

4 Comments

I forgot the [] notation. Would you use for or foreach?
I might try this then: foreach ($_POST['idd'] as $k => $v) { $sql="UPDATE eventi SET categoria='$categoria' WHERE id='$id'"; $update=mysql_query($sql); }
Not $categoria but $_POST['categoria'][$k]. Same to $id.
I will test it and post the results.
0

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.