0

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

2
  • 1
    do you have any primary key in your MYSQL table ? Commented May 3, 2015 at 17:48
  • "That makes sense since the current value of the table key is the last row that was read" - That statement doesn't make any sense to me. Can you show the actual code you're using and how you're trying to update the data? If each table row has its own independent HTML form and each form includes an identifier for the database row then you'd simply use that identifier when updating the table. I don't see how this could not work. Commented May 3, 2015 at 17:52

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.