I created the following code to populate a table with data stored in database.
As you can see the data are also directly editable in the fields, what I am trying to do is to save the edited field(s) to the database.
If the field has been modified just overwrite the “old” field, if it has not been modified take the old field.
<?php
$querymod =" SELECT * FROM table ORDER BY id DESC ";
$result = mysql_query($querymod) or die(mysql_error());
echo "<div style='width: 100%; text-align: center;'>";
echo "<table style='margin: auto auto;'>";
echo "<tr><th>ID</th><th>Image</th><th>Article Number</th><th>Description</th></tr>";
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$img_name = $row['img_name'];
$art_number = $row['art_number'];
$description = $row['description'];
echo "<form method='post' action=''>";
echo "<tr>
<td style='width: 20px;'><input name='id' id='id' value='".$id."' disabled='yes'>
<input type='submit' name='submit' value='Save'></td>
<td style='width: 210px;'><img src='../../upload/content/uploads/". $img_name ."' width='200px'></td>
<td style='width: 100px;'><input type='text' name='art_number' value='".$art_number."'></td>
<td style='width: 100px;'><input type='text' name='description' value='".$description."'></td>
</tr>";
}
echo "</table><br /><br /></div>";
echo "</form>";
?>
I know that with the “UPDATE” function I can update the database and the fields, but the problem is that I don’t know how to get the ID of the modified row, and start the update of the related modified field.
Any hint please?
You need to move:
inside the while-loop. You’re starting a new form each time through the loop, but not closing it until the entire loop is done. This is creating incorrect nesting.
Also, get rid of
id='id'(you probably don’t need it) or do something to ensure that the IDs are unique.