1

Ok, i have a checklist system that i am working on. There is a page that pulls data from the database and displays it as a checklist. I am trying to create a button that when pushed will reset the database to a certain state that i want. Basically, i have a button that sends an ajax callout to a php page that executes an UPDATE query. This query is as follows:

UPDATE $table SET value='$value', comments='$comments', editedBy='$editedBy', editedDate='$editedDate' WHERE projectId='$projectId';

I set the variables first of course, that's not my question. Just pretend they have data. My question is how can i repeat this query so that every row table x that has a projectId of n is updated? I'm guessing this involves a for loop?

SIDE NOTE: Since this query is just setting the value to false and making the comments, editedBy, and editedDate fields blank for every row in table x that has a projectId of n, is there a better way of doing this other than the UPDATE query?

Thanks for any help!

3
  • I'm assuming that the problem here is that you do not know all the rows table X has which makes it troublesome to update or am I wrong? Commented Apr 20, 2011 at 14:53
  • 1
    This query updates all rows with the projectId you supply. Commented Apr 20, 2011 at 14:54
  • thanks @edem, as you and @Jesse said, i'll just give that query a try on its own. Commented Apr 20, 2011 at 15:00

1 Answer 1

2

As long as you don't sepcify a LIMIT in your UPDATE query, it will update every row it finds that satisfies your where clause.

Now, if you're updating the projects table and projectID is your primary key, you'll need to run a loop to update other projectIDs. If you're not updating the project table, then your update query will update any record that has a foreign key match to the project ID you specified.

Does that help?

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

3 Comments

thanks! I was thinking that might be the case but i didn't want to just run the query and screw up the database..wouldn't be the end of the world, just more work. Thanks for the help. I'll give it a try.
yea, it worked! Guess i knew what i was doing after all. Thanks for confirming it for me. Now, is there a better way of removing data from a column on a specific row other than updating the row with a blank value? Or does this suffice?
Depending on your constraints, you could set the value = NULL in your SQL query. However, a blank value should suffice in most applications.

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.