eMonk Posted June 10, 2011 Share Posted June 10, 2011 I have an update form where users can update their info. If they update their email address, I want "to do" something. For example: if(isset($email) ) { do something } However the isset function won't work in my case because $email is always NOT NULL so it will always perform this action which won't be necessarily. What function do I use to check if $email was changed? Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/ Share on other sites More sharing options...
eMonk Posted June 10, 2011 Author Share Posted June 10, 2011 Something like this? if $email = $email { perform normal query } else { perform a modified query } Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228095 Share on other sites More sharing options...
The Little Guy Posted June 10, 2011 Share Posted June 10, 2011 use empty Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228097 Share on other sites More sharing options...
Fadion Posted June 10, 2011 Share Posted June 10, 2011 When the form is submitted, check if the email isn't empty (via $_POST or $_GET) and if not, update the data in database. There's no need to compare the form value with the actual value from database, because if the email isn't empty, it is supposed to be changed. <?php $email = trim($_POST['email']); if ($email != '') { $results = mysql_query("UPDATE users SET email='$email' WHERE user_id=10"); } ?> Hope I got it right, anyway. Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228098 Share on other sites More sharing options...
The Little Guy Posted June 10, 2011 Share Posted June 10, 2011 When the form is submitted, check if the email isn't empty (via $_POST or $_GET) and if not, update the data in database. There's no need to compare the form value with the actual value from database, because if the email isn't empty, it is supposed to be changed. <?php $email = trim($_POST['email']); if ($email != '') { $results = mysql_query("UPDATE users SET email='$email' WHERE user_id=10"); } ?> Hope I got it right, anyway. the empy function is better Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228099 Share on other sites More sharing options...
eMonk Posted June 10, 2011 Author Share Posted June 10, 2011 email will always be not empty. For example, let's say John wants to update his email address. The update form loads john@somewhere.com into the form but then he changes it to johnny@somewhere.com. If this is the case then I'll need to update the members database as well with his new email address. Sure I can always perform this action and override it everytime but I don't think it's necessarily? Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228103 Share on other sites More sharing options...
eMonk Posted June 10, 2011 Author Share Posted June 10, 2011 Or maybe just not have the form fetch their email address into the email text field and if they fill this field in then I'll update the members database as well? Then I can use the isset function and let them know only to fill in this textbox if they want to update their email address and display their old one below it. Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228104 Share on other sites More sharing options...
eMonk Posted June 10, 2011 Author Share Posted June 10, 2011 Ah, guess I will override it every time a form is updated since the other values are being overwritten as well. Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228106 Share on other sites More sharing options...
Pikachu2000 Posted June 10, 2011 Share Posted June 10, 2011 If the value of a field value in the UPDATE query is the same as what's in the field already, MySQL won't do anything with it anyhow. http://dev.mysql.com/doc/refman/5.0/en/update.html Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228111 Share on other sites More sharing options...
eMonk Posted June 10, 2011 Author Share Posted June 10, 2011 Thanks for the info Pikachu2000 and everyone else that commented. Question: What's the difference between (if any): if (isset($email)) and if (empty($email)) Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228134 Share on other sites More sharing options...
eMonk Posted June 10, 2011 Author Share Posted June 10, 2011 Nevermind. I see a comparison in example #1 at: http://ca3.php.net/empty I learned a new function today. Thanks The Little Guy! Quote Link to comment https://forums.phpfreaks.com/topic/239016-if-a-value-is-changed-what-function-do-i-use/#findComment-1228136 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.