doubledee Posted April 15, 2012 Share Posted April 15, 2012 Not sure if there is a better way to do this, but I want to check whether the value in a Sticky Field is the same as what is in the Database. If what is in the Sticky Field is *exactly* what is in the Database, then the User didn't update that field, and so I would NOT do an UPDATE. Seem reasonable? Here is where I grab the value out of the Form... // **************************** // Process Each Form Field. * // **************************** foreach($_POST['answerArray'] as $questionID => $response){ // Copy trimmed Form array to PHP array. $answerArray[$questionID] = trim($response); And here is where I grab the existing value in the Database... // ******************** // Check for Answer. * // ******************** // Build query. $q1 = "SELECT response FROM bio_answer WHERE member_id=? AND question_id=?"; // Prepare statement. $stmt1 = mysqli_prepare($dbc, $q1); // Bind variable to query. mysqli_stmt_bind_param($stmt1, 'ii', $memberID, $questionID); // Execute query. mysqli_stmt_execute($stmt1); // Store results. mysqli_stmt_store_result($stmt1); // Check # of Records Returned. if (mysqli_stmt_num_rows($stmt1)==1){ // Answer Found. // Bind result-set to variable. mysqli_stmt_bind_result($stmt1, $currResponse); // Fetch record. mysqli_stmt_fetch($stmt1); So I guess I want to compare $answerArray[$questionID] against $currResponse and see if there are any differences?! What is the best way to do that? Any better ideas of how to check to see if the User made any changes to a field so I only do an UPDATE if there was actually a change? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/260958-how-do-i-compare-strings/ Share on other sites More sharing options...
kicken Posted April 15, 2012 Share Posted April 15, 2012 You can just do the update regardless. Checking if it's changed or not just to avoid an update query is not worth while. MySQL will already check during it's update process if the field value has actually changed, and if not skip updating that field. If you set a column to the value it currently has, MySQL notices this and does not update it. Quote Link to comment https://forums.phpfreaks.com/topic/260958-how-do-i-compare-strings/#findComment-1337458 Share on other sites More sharing options...
algidDes702 Posted April 15, 2012 Share Posted April 15, 2012 Kicken is right there. If there is some other reason that won't work for your situation you can use strpos to find a very exact string. If its not found a false will be returned. However, if the search finds that exact string and there is more to the left or right it will still return the position of that match. I found this quickly and I'm sure there are functions that are purely Boolean and return a true or false instead of false or a position, search php.net. Thanks, AlgidDes Quote Link to comment https://forums.phpfreaks.com/topic/260958-how-do-i-compare-strings/#findComment-1337462 Share on other sites More sharing options...
Psycho Posted April 15, 2012 Share Posted April 15, 2012 . . . you can use strpos to find a very exact string. If its not found a false will be returned. However, if the search finds that exact string and there is more to the left or right it will still return the position of that match. I found this quickly and I'm sure there are functions that are purely Boolean and return a true or false instead of false or a position, search php.net. You are correct that there are more appropriate functions. strcmp(), short for "string comparison" will do exactly as you suspected. strcmp — Binary safe string comparison Quote Link to comment https://forums.phpfreaks.com/topic/260958-how-do-i-compare-strings/#findComment-1337464 Share on other sites More sharing options...
doubledee Posted April 15, 2012 Author Share Posted April 15, 2012 This is what I ended up doing... // ******************************** // Check for Changes to Answer. * // ******************************** if (strcmp($answerArray[$questionID], $currResponse)==0){ // Answer Unchanged. echo '<p>Field #' . $questionID . ' has NOT changed.</p>'; }else{ // Answer Changed. echo '<p>Field #' . $questionID . ' has changed.</p>'; // ****************** // Update Answer. * // ****************** // Build query. $q2 = "UPDATE bio_answer SET response=?, updated_on=NOW() WHERE member_id=? AND question_id=? LIMIT 1"; But according to Kicken, that was unnecessary, I guess?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/260958-how-do-i-compare-strings/#findComment-1337465 Share on other sites More sharing options...
kicken Posted April 15, 2012 Share Posted April 15, 2012 You will need to do your comparison check if you want to prevent the updated_on field from being changed when there was no content changes. MySQL would still update that even if the response value had not changed. Quote Link to comment https://forums.phpfreaks.com/topic/260958-how-do-i-compare-strings/#findComment-1337467 Share on other sites More sharing options...
doubledee Posted April 15, 2012 Author Share Posted April 15, 2012 You will need to do your comparison check if you want to prevent the updated_on field from being changed when there was no content changes. MySQL would still update that even if the response value had not changed. Yep, that was the whole point!! Okay, so it looks like my extra effort paid off. Thanks!! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/260958-how-do-i-compare-strings/#findComment-1337468 Share on other sites More sharing options...
Psycho Posted April 15, 2012 Share Posted April 15, 2012 You will need to do your comparison check if you want to prevent the updated_on field from being changed when there was no content changes. MySQL would still update that even if the response value had not changed. Which is why an "updated_on" field should be set up as a "TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" so the updated timestamp would be handled by the database. Then there is no need to include a timestamp in the UPDATE queries and if the UPDATE query would not result in any changes, the timestamp is not updated either. That is the simplest solution. Okay, so it looks like my extra effort paid off. Sorry, but no. Quote Link to comment https://forums.phpfreaks.com/topic/260958-how-do-i-compare-strings/#findComment-1337471 Share on other sites More sharing options...
doubledee Posted April 15, 2012 Author Share Posted April 15, 2012 You will need to do your comparison check if you want to prevent the updated_on field from being changed when there was no content changes. MySQL would still update that even if the response value had not changed. Which is why an "updated_on" field should be set up as a "TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" so the updated timestamp would be handled by the database. Then there is no need to include a timestamp in the UPDATE queries and if the UPDATE query would not result in any changes, the timestamp is not updated either. That is the simplest solution. Hey, that's pretty cool! I learned something new! Okay, so it looks like my extra effort paid off. Sorry, but no. Oh well. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/260958-how-do-i-compare-strings/#findComment-1337476 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.