如何在显示多行时更新mysql行

I have a php 5.5 website using mysql 5.5.

I have read a number of rows from a mysql table and displayed the data on a html page where the user can edit it.

How do i ensure that I update the correct row with the new data from the user?

For example, if the user updates the data in field 3 of row 6 then I need to update field 3 of row 6 on the database.

I tried having a submit button (with the same name) on every row but found that it doesn't matter which row the user changes, the last row on the table is updated.

That makes sense since the current value of the table key is the last row that was read but that doesn't help me at all...

Any ideas? Thanks in advance

When you query the DB, use:

    "UPDATE SET your_field = 'new_value' WHERE changed_row= 'changed_row'"

According to W3Schools:

Notice the WHERE clause in the SQL UPDATE statement! The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

Hope that helpes?

I need to update field 3 of row 6 on the database

There is no field 3, and there is no row 6. Your database doesn't guarantee sort order of anything unless it's explicitly specified in a query on that data.

So you don't need to update "field 3", you need to update an identified field:

UPDATE SomeTable SET SomeField = 'some value'

Notice that the field is explicitly named, it's not updated by some cardinal position in relation to other fields. Similarly for rows, you need to identify the row(s) which should be updated:

UPDATE SomeTable SET SomeField = 'some value' WHERE SomeOtherField = 'some other value'

The row(s) is/are identified by some logical identifier, not by their cardinal position in relation to any other rows.

Without seeing any of your code or table structure it's hard to really be any more specific than that. But what this essentially means to your HTML/PHP is that when the form is posted to the server with values to update, it needs to include some way of identifying what needs to be updated. For example, one row in your HTML table might contain this:

<form action="somePage.php">
    <input type="hidden" name="id" value="1" />
    <input type="text" name="SomeField" value="some value" />
    <input type="submit" value="Submit" />
</form>

Then in your server-side code you would construct your query to use the value of $_POST['SomeField'] in the UPDATE statement, and use the value of $_POST['id'] in the WHERE clause for that UPDATE statement.