50r Posted November 14, 2012 Share Posted November 14, 2012 Hello freaks i have a problem here which is not so big but tricky though. i have a form with about 30 fields that i have to use to run a update to the database. the form populates the existing data from the database when it is called. then what i want is i before fields are submited to database for update, all fields must be checked to see weather any is still the same as the exsting onein the database and if its found, the update on that field must not run it should move to the next field. i know i can check one by one fields to see using the if and else but as fields are so many i am going to end up with a big banch of code. is there any simple way i can tackle this? Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/ Share on other sites More sharing options...
Jessica Posted November 14, 2012 Share Posted November 14, 2012 If the field is the same, why does it matter if it updates it or not? Are you talking about ROWS or Fields/Columns? Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392345 Share on other sites More sharing options...
50r Posted November 14, 2012 Author Share Posted November 14, 2012 does that mean there is no simple way i can comapare data in such a way? i am talking about rows but in that row i want someone can only be able to edit records that if there is a change he /she made. Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392347 Share on other sites More sharing options...
Jessica Posted November 14, 2012 Share Posted November 14, 2012 Now your question is even less clear. You have 30 rows of data. You want people to edit only rows they can edit? What? Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392349 Share on other sites More sharing options...
Manixat Posted November 14, 2012 Share Posted November 14, 2012 I believe he meant he only wants rows to be overwritten if there are any changes made, which is unnecessary but either ways you can just store the database values and the input values in 2 arrays and compare them then remove the matching items Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392351 Share on other sites More sharing options...
50r Posted November 14, 2012 Author Share Posted November 14, 2012 1 row with 30 fields Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392359 Share on other sites More sharing options...
AyKay47 Posted November 14, 2012 Share Posted November 14, 2012 So each form field is responsible for updating its respected db table field, and if a certain form field is the same as the db table field then don't update? Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392363 Share on other sites More sharing options...
Jessica Posted November 14, 2012 Share Posted November 14, 2012 As Manixat said, put the original data in one array, the new data in another, and compare them. If they differ, add it to an array of values to update (or remove it from the new data array). Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392364 Share on other sites More sharing options...
50r Posted November 14, 2012 Author Share Posted November 14, 2012 yeah the post data is already in the $_POST array and the database data is in the $row array but the question is how do i campare them and how do i exclude array items that are the same. Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392389 Share on other sites More sharing options...
mikosiko Posted November 14, 2012 Share Posted November 14, 2012 ...and you next problem is going to be to construct EVERY TIME a dynamic UPDATE based just on the columns that were modified... absolutely unnecessary and cumbersome, but just out of curiosity... why do you want to do that?.... maybe a better explanation could bring some light and more help to get your objectives in an alternative way Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392392 Share on other sites More sharing options...
Manixat Posted November 14, 2012 Share Posted November 14, 2012 (edited) ...and you next problem is going to be to construct EVERY TIME a dynamic UPDATE based just on the columns that were modified... not entirely true. You can foreach the $row array and compare it with the $_post array, if there is a match unset the $row element. Then do a foreach($row) to set a string with the keys ( field names ) and another string with the values which can also be achieved via implode(). Then just join the 2 strings in the UPDATE query. It is important that your $_POST and $row array keys are the same for your ease. Edited November 14, 2012 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392394 Share on other sites More sharing options...
mikosiko Posted November 14, 2012 Share Posted November 14, 2012 @manixat : what exactly is "not entirely true"?... you are explaining exactly the process that I mean... cumbersome .. overly complicated when in reality is not needed, a simple and direct UPDATE without care if an specific column has changed or not will do the same job ... just easily... granted the process that you described is the way to do it if the OP decide to go that route. But as I said by the end of my previous post, we need to know more about the OP objectives to determine if is justifiable or not. Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392428 Share on other sites More sharing options...
PFMaBiSmAd Posted November 14, 2012 Share Posted November 14, 2012 array_diff_assoc will give you keys/values that are different between an original data array and the submitted data from the form. Quote Link to comment https://forums.phpfreaks.com/topic/270681-checking-update-data-if-it-does-not-exist-in-the-database/#findComment-1392436 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.