I have a MySql table and I would to be able to modify through a HTML form. The MySql table has columns like this:
id (int) | name (text) | option1 (boolean) | option2 (boolean)
The number of field options
will grow over time and is not fixed, but always contains boolean data (0 or 1). The number of rows is not fixed as well.
I would like to modify the table with a html form which looks like this:
<form action="file.php" method="post">
<table style="width:100%">
<tr>
<th>Name</th>
<th>Option1</th>
<th>Option2</th>
</tr>
<tr>
<th>lili</th>
<th><input type="checkbox" name="lili_1"checked></th>
<th><input type="checkbox" name="lili_2"></th>
</tr>
<tr>
<th>thor</th>
<th><input type="checkbox" name="thor_1" checked></th>
<th><input type="checkbox" name="thor_2" checked></th>
</tr>
<tr>
<th>fred</th>
<th><input type="checkbox" name="fred_1" checked></th>
<th><input type="checkbox" name="fred_2" ></th>
</tr>
</table>
<button type="submit">Update</button>
</form>
Now it gets tricky. When I submit the form, on the server side I don't know how many columns/lines there are. I would like to update the whole database table (it's small), so I need to know the state of every checkbox for columns (option1, option2, ...) and rows (lili, thor, fred,...)
My idea is to name each checkbox based on the column and value of field "name". I would also need to send an array containing all the names and the columns (because I can only know the boxes that were checked, not the unckecked ones).
On server-side, I then need to recreate the matrix with the array of columns and names and put 1 where a checkbox was checked (through the name of the checkbox).
This seems to be a very error-prone and long to implement... to do such an simple task. Can someone points me to a smarter way to to that ?
Thank you