Jump to content

updating records


jimfog

Recommended Posts

suppose we have a 3 field form which the user wants to edit-in other words, updating the corresponding records in the database. AM i right to assume that the max queries that the above action might require is three and the minimum is one? Just I want to see if my assumptions are correct here.

Link to comment
Share on other sites

If the fields are in the same table then you only need 1.
Yes...but what if the user wants to edit only one field from the form.Suppose the form has name, lastname and age. Again it will be 1 query... But what if he wants to edit 2 fields-name and last name for example. My opinion is that I must prepare 3 queries and use conditionals to choose when to use each one of them-depending what the user wantsto edit/change. I am just as wondering if there is a shortcut here. WHat do you think?
Link to comment
Share on other sites

One option is to build the query dynamically to only include the columns that were updated, another option is to just include all of them regardless of what they actually changed, if anything. It's not going to cause the database to fail if you tell it to update a field with the same value that's already there. If you want to build a conditional statement then you need to write more than 3 queries, which is just not efficient programming. If the fields are A, B, and C, then your plan would be to write separate queries to update these combinations: ABCA, BA, CB, CA, B, C So instead of writing 7 queries and doing a bunch of unnecessary checks, most of the time I just update all fields with what was submitted. If I want to determine what they changed then I get the current values from the database, get the values they submitted, and use array_diff to find the differences.

Link to comment
Share on other sites

THIS IS WHAT I WAS EXPECTING TO LISTEN. Writing all these queries is NOT efficient programming.I might post again about it since I have never used array_diff and some help might be required. And something else...in what case we would want to see the difference between what is submittedand what is in the db? Just give me an example-nothing more. Thanks

Edited by jimfog
Link to comment
Share on other sites

And something else...in what case we would want to see the difference between what is submitted and what is in the db?
You can't think of anything where that might be useful? Maybe you want to make a log of what was changed, maybe you need to do other actions based on whether specific things were changed, etc. It's up to the application designer to create those features, I'm just saying that it's possible to do it.
Link to comment
Share on other sites

And something else...in what case we would want to see the difference between what is submitted and what is in the db?
Isn't what you're trying to do already an example? Diff what was submitted vs. what is in the DB and dynamically build the UPDATE query?
Link to comment
Share on other sites

Diff what was submitted vs. what is in the DB and dynamically build the UPDATE query?
How am I going to dynamically build the update query? Can you give a code example using array_diff?My current method is to update all the fields again regardless if all of them actually have been updated by the user.The form fields that were not updated/edited by the user will be re-entered(updated) in the db as they were. Unless you have to propose a different method.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...