Jump to content

if a value is changed... what function do i use?


eMonk

Recommended Posts

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?

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.

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

email will always be not empty.

 

For example, let's say John wants to update his email address.

 

The update form loads [email protected] into the form but then he changes it to [email protected]. 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?

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.