Skip to content
Advertisement

Getting id from a textarea in a while loop in PHP for updating in mysql

I have a table where is one week displayed (each row is one day). I get the rows from a while loop from my database. The rows are displayed in bootstrap accordions. There is a textarea in every accordion row where the user can input (update) some text.

I want to update this text into my database. It should update the text depending on the day id.

    <form method="POST" action="">
    <table class="table table-hover" style="border-collapse:collapse;">
        <thead>
                <tr>
                        <th>Weekday</th>
                        <th>Date</th>
                        
                </tr>
        </thead>
        <tbody>
<?php
// Select Statement (for shortening not included into this Stack question)//

    while($row = $statement->fetch()) {
    $thedate = $row['Date'];
    $weekday=strftime("%A", strtotime($thedate));
    $date=date('d-m-Y', strtotime($thedate));

        echo "<tr data-toggle='collapse' data-target=#".$row['Date']." class='clickable collapse-row collapsed'>";
        echo "<td >".$weekday."</td>";
        echo "<td>".$date."</td>";
        echo" <td style='color:black; font-size:20px;'><i class='fas fa-angle-down'></i></td>";
        echo "</tr>";

echo "<tr><div class='accordian-body collapse' id=".$row['Date'].">
<td colspan='1' class='hiddenRow'><textarea name=".$row['id']." rows='5' cols='80'>".$row['Text']." </textarea></td>

//the $row['id'] should give every textarea a unique dayid from my database

echo"</td>
</div></tr>";
}
    if(ISSET($_POST['id'])){
    $debug=$_POST['id'];
    }
var_dump($debug); // var_dump for debugging. See text below

        ?>
</tbody>
    </table>
    <button type="submit" name="Speichern" class="btn btn-lg btn-primary btn-block">Speichern</button>
    </form>

Before writing the Sql Update Statement I wanted to debug to find possible bugs.

If i debug this with var_dump I get the error message “Undefined variable $debug” and I dont know why. The variable shouldnt be empty because in the textareas is always text. Im new to PHP and coding at all so probably Im making a dump mistake.

EDIT: If I put the var_dump inside the if condition i get nothing as return.

Advertisement

Answer

I tried it also with the var_dump in the if block but then i get nothing as return.

That’s because you do not have any form field that is actually named id. You put name=".$row['id']." on your textarea, and that is likely a numeric value. And you probably don’t know which one that will be, on the receiving end.

Plus, since you are creating multiple such fields in a loop, PHP will overwrite all values for this parameter with the last one. You need to use a naming scheme that includes square brackets to avoid that, something like name="foo[]" – then $_POST['foo'] will become an array that you can loop over.

And since you will still need your record ID to associate with the data, you can put that into the brackets, name="foo[123]" – then this 123 will become the key of that array element, for this specific textarea.

If you loop over that using the extended foreach syntax, then you have easy access to the ID, and the value entered by the user:

foreach( $_POST['foo'] as $id => $value ) { … }
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement