If already exist

hi every one, am having a bit of an issue in my already exist profile page.
The thing is if a user checks his/her profile page to reedit their profile and click save the already exist code will kick in not allowing the user to save and showing the already exist error msg for the email instead of just ignoring and should only work if the user tries to change his/her email to another email that already exist to show the error hope u understand below is the code i wrote up funny thing the code works in a weird way if i try inserting the 1st email in the column that isnt mine i get already exist but if i try others it get updated


            $query = "SELECT email FROM " . $DBPrefix . "users WHERE id = id IN (SELECT id FROM " . $DBPrefix . "users     
            
            WHERE email = :email)";
            $params = array();
            $params[] = array(':email', $system->cleanvars($_POST['TPL_email']), 'str');
            $db->query($query, $params);
            if ($db->numrows() > 0)
            {
                $ERR = $ERR_115; // E-mail already used
            }

thanks yo.

I would place a UNIQUE constraint on the email field, so the database automatically rejects any duplicate emails. no further verification necessary.

Not sure exactly what you have going on, but a profile update is a simple task.

The logged in users ID should already be in a SESSION, then you simply do an

UPDATE users SET whatever_column = ‘whatever’ WHERE user_id = $_SESSION[‘user_id’].

yeap worked fine that way thanks

ok haven done that am left with abit of an issue, when some rows are empty it comes back as already exist.
isnt a blank row ought to be ignore?

code below am using for already exist

`$query = "SELECT phone FROM " . $DBPrefix . "users WHERE id <> :user_id and phone = :phone";
$params = array();
$params[] = array(':phone', $system->cleanvars($_POST['TPL_phone']), 'str');
$params[] = array(':user_id', $user->user_data['id'], 'int');`

how do i make it ignore blank rows

If you mean that the phone column can be blank, if the user attempts to change their phone number to a blank then obviously you cannot check for duplicates in that column. You would just need to modify your code to not run the query for any columns that are blank.

yeah i need for them to be able to change it to blank but how do i rewrite d code for that have tried using null but doesnt work

Just check to see whether $_POST['TPL_phone'] is null, and don’t run the query if it is. Same for any other column that can be empty / blank / duplicated.

nope nothing

and since d columns are null by default and a blank value is submitted isnt there a way for the column to retain its null

Hard to offer any assistance with this kind of feedback. Could you show the code you added for checking if the variable is blank?

It would do, if your update query only specifies the new values for the columns that have changed. I thought the problem you were having here was in the duplicate-check prior to committing the profile update?

problem am actually having is the blank column being seen as a value thus returning an already exist error.

But my point is that you shouldn’t be checking for duplicates if the entry is blank.

$query = "SELECT accnumber FROM " . $DBPrefix . "users WHERE id <> :user_id and accnumber = :accnumber and accnumber IS NULL";
$params = array();
$params[] = array(':accnumber', $system->cleanvars($_POST['TPL_accnumber']), 'str');
$params[] = array(':user_id', $user->user_data['id'], 'int');

As with the other code (the phone number) that you posted, if you allow blanks in these columns, then these queries will return results. So if blanks are the only type of duplicates allowed, don’t run these checks when the entry is blank. Or run the checks, but only trigger your error code if there are results and the data to check is not blank.

might giving a hint or startup on how i should run d code, thanks

Before your query code:

if (!empty(your-variable-name)) {

and after you have run the query and processed the results

}

This means it won’t run the query if the variable name (your $POST variables) has something in it.

i suppose it will look like this

if (!empty(accnumber)) {


$query = "SELECT accnumber FROM " . $DBPrefix . "users WHERE id <> :user_id and accnumber = :accnumber ";
$params = array();
$params[] = array(':accnumber', $system->cleanvars($_POST['TPL_accnumber']), 'str');
$params[] = array(':user_id', $user->user_data['id'], 'int');
$db->query($query, $params);
    if ($db->numrows() > 0)
            {
                $ERR = $ERR_115acc; 
            }    
                    
}    

}

right

Something like that, but you need to use the $_POST variable in your if statement. You’ll just get a syntax error the way you’ve written it there.

yeah included it now works like charm so far so good.
thanks alot droop