1

I'm coding an html table that displays information from a MySql table. Each row is a series of input's so the values of the table can be easily updated.

Here's my current code:

<form action=index.php/component/studentmanagement/?task=update method=post>

<table>
    <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Program</th>
        <th>Class</th>
    </tr>

    <?php

    $db = JFactory::getDBO();

    $query = "SELECT * FROM  student_management_module";

    $db->setQuery($query);
    $rows = $db->loadObjectList();

    foreach ($rows as &$row) {
        echo "<tr>";
        echo "<td>" . "<input type=text name=fullName id=name_val value=" .$row->name. "> </td>";
        echo "<td>" . "<input type=text name=email id=email_val value=" .$row->email. "> </td>";
        echo "<td>" . "<input type=text name=prog id=prog_val value=" .$row->program. "> </td>";
        echo "<td>" . "<input type=text name=class id=class_val value=" .$row->class. "> </td>";
        echo "<td class = 'headcol'> <input type=submit name=update class='btnupdate' value=update>";
        echo "<td>" . "<input type=hidden name=hidden value=" .$row->student_id. "> </td>";
        echo "</tr>";
    }

    ?>

</table> </form>

But whenever I try submitting the updated values, they don't get pass to my update functions. Am I putting the table in the form correctly?

Thanks in advance, and I'll appreciate any help.

This is my update function:

<?php
    $db = JFactory::getDBO();
$query = "UPDATE student_management_module SET name = '$_POST[fullName]', email = '$_POST[email]', program='$_POST[prog]', class='$_POST[class]' WHERE student_id='$_POST[hidden]'";
$db->setQuery($query);
$db->query();

?>

Kilter
  • 41
  • 5
  • 1
    You are using IDs multiple times, which is invalid HTML. And you are using the same names for the form fields multiple times, which will make PHP overwrite each parameter with only the last value received - unless you use a naming scheme including `[]` to generate an array. More info on that, https://www.google.com/search?q=php+form+fields+only+last+value+site:stackoverflow.com – CBroe Jul 08 '18 at 02:53
  • @CBroe - Yep, totally right. Removing my answer. – Tigger Jul 08 '18 at 02:57
  • @CBroe so should I be using a class for each input instead of IDs? – Kilter Jul 08 '18 at 03:11
  • Depends on what for/how you need to be able to select or identify them. Do you need to do something specific with them using either JS or CSS ...? Othwerwise, for the purpose of form data submission, the name (and type) are all that's really needed. – CBroe Jul 08 '18 at 04:56
  • yea, I wanna add css to style them – Kilter Jul 08 '18 at 18:41
  • @CBroe how can I send only the info of the row that I want to update and not the info of the entire table? – Kilter Jul 08 '18 at 19:30
  • That is not directly possible, at least not with this code. A form can only either go around the whole table element, or it must be contained within a table cell. It can not be inserted at a row level. You would either need a different HTML structure that allowed for the inserting of a form around such a “row”/group of input fields, or you’d have to add that kind of functionality via JavaScript. – CBroe Jul 09 '18 at 06:36

1 Answers1

0

Try having the value of the name and id in quotes or double quote. I.e., id="class_val". In your case, since you have already inserted the td in a double quote, use single quote. So it will be "<td class='foo' id='foo' name='foo'></td>".

Karma Blackshaw
  • 880
  • 8
  • 20