I'm creating a page that searches for an item and then be able to edit/update it. I was able to do it when it returns just one result but when it gives me multiple results I could only edit the very last item. Below is my code:
.......
$dj =$_POST[djnum];
$sql= "SELECT * From dj WHERE datajack LIKE '$dj%'";
$result = mysql_query($sql);
//more code in here//
while ($info =mysql_fetch_array($result)) {
// display the result
echo "<form action=\"dj_update.php\" method=\"POST\"><input type=\"hidden\" name=\"djnumber\" value=\"".$info['开发者_如何学Pythondatajack']."\">";
echo "<tr><td>DJ ".$info['datajack']."</td>";
echo "<td>".$info['building']." </td>";
echo "<td>Rm ".$info['room']." </td>";
echo "<td>".$info['switch']." </td>";
echo "<td>".$info['port']." </td>";
echo "<td>".$info['notes']." </td>";
echo "<td style=\"text-align:center;\"><input type=\"Submit\" value=\"Edit\" ></td></tr>";
}
// more code here //
Then this is the screen shot of the result:
The idea is the user should be able to click on "Edit" and be able to edit/update that particular item. But when I click any of the Edit button I could only edit the last item. What am I missing here? Is there an easier way to do this?
Thanks guys and Happy new year!
There's no form closing tag - it should be added after each "Edit" button.
Now, since forms are not closed, there are several hidden inputs with the same "djnumber" in each form, and I suppose, browser sends only one value - which is specified in your last row.
So, adding the following at the end of your loop:
echo "</form>";
should help.
What are the values for datajack
?
If the values are datajack1
, datajack2
etc then a LIKE
will return the first one every time, you need to make your query more specific.
$sql = "SELECT * From dj WHERE datajack id ' " . mysql_escape_string($id) . "' LIMIT 1";
I have changed the query to match one row and also use an id field, using LIKE
in this situation is bad, you want to edit a specific row, not a row that is potentially LIKE
the row you thought you were editing.
Note the use of mysql_escape_string()
too to stop MySQL Injection techniques.
Hope that helps.
精彩评论