Page 1 of 1

How to check if a value in a form is different from db

Posted: Sun Jan 30, 2011 9:26 am
by tsalaki
What I want to ask is if there is a way to check whether a value in a form element is different from the value in the database. What I ideally want to do is the following:
I have a form that has the default values from my database. If the user change these values I want to update them. If the user doesn't change them there is no need to update this field in my database. Can I do this? And if there is a way to check this out, is it worth to do something like that, or it doesn't matter at all if i "update" with the same value that already exists in my database.

Forgive me for my question I am new to php. Thanks in advance.

Re: How to check if a value in a form is different from db

Posted: Sun Jan 30, 2011 9:37 am
by kr1pt

Code: Select all

if ($_POST['some_data'] == $data['some_data_from_db'])
{
    return false;
}
If you want full code, just say ;)

Re: How to check if a value in a form is different from db

Posted: Sun Jan 30, 2011 9:50 am
by tsalaki
No I don't want full code..Please tell me that:
I have in my form when I define it something like that:

Code: Select all

<form name="specified name" action="save-changes.php" method="post">
When the user presses the submit button in my save_changes.php I have to create a connection to my database, take the values from my database and then make an if statement just like this you have posted?

I am asking that because in the .php file I have created my form I make a connection to my database and I post the values from my database to the form. These values are my default. I try to store these values in a $SESSION variables but it doesn't work.

Re: How to check if a value in a form is different from db

Posted: Sun Jan 30, 2011 10:01 am
by kr1pt

Code: Select all

// edit.php
echo '<form name="specified name" action="save-changes.php" method="post">';
echo 'Some form code';
echo '</form>';

// save-changes.php
if (isset($_POST['submit']))
{
    // If you have ALOT of fields in DB, just shortcut it like I do
    foreach (array('username', 'email', 'something_from_db') as $data)
    {
        if ($_POST[$data] == $row[$data])
        {
            return;
        }
        else
        {
            $sql = mysql_query('UPDATE users SET ' . $data . ' = ' . mysql_real_escape_string($_POST[$data]) . ' WHERE user_id = ' . $some_user_id . '');

            if (! $sql)
            {
                die(mysql_error());
            }
        }
    }
}
else
{
    echo 'Form is not submitted.';
}

Re: How to check if a value in a form is different from db

Posted: Sun Jan 30, 2011 10:23 am
by tsalaki
Thanks a lot for your script. I have fully understand it I think. The only think I want to ask is the $row[data] you use in your if statement defines actually every value taken from the table in my database?
My form is a little more complicated and requires a little work for me in order to make it work as I want but thanks a lot for your response and your script. I really appreciate your help!