Check if the record already exists, if it exists does not change the value (MYSQL)

Asked

Viewed 423 times

0

PHP functions

<?
if($_REQUEST['salva_ed']){
    $sqlu="update registration set username='$username', firstname='$fname',  lastname='$lname', sex='$gender',
            birth_date='$bdate', addressline1='$address1',addressline2='$address2', complemento='$address3', numero='$address4', city='$city',  state='$state',
            country='$country', postcode='$zipcode', phone='$phone', mobile_no='$mobileno', full_mobileno='$fullmobile',
            email='$email', password=MD5('$pass'), cpf='$cpf',rg='$rg'  where id='$id'";
    $resultu=mysql_query($sqlu);
    header("location:message.php?msg=3"); exit;
}


if($_REQUEST['salva_nv']){
    $verifcode = md5($username);
    $agora=Date("Y-m-d H:i:s");
    $sqlu="insert into registration (username,firstname,lastname,sex,birth_date,addressline1,addressline2,city,state,
            country,postcode,phone,mobile_no,full_mobileno,email,password,cpf,rg,register_date,verify_code,
            final_bids,account_status,member_status,user_delete_flag,sponser,sms_flag,admin_user_flag) values
            ('$username','$fname','$lname','$gender','$bdate','$address1','$address2','$address3','$address4','$city','$state',
            '$country','$zipcode','$phone','$mobileno','$fullmobile','$email',MD5('$pass'),'$cpf','$rg','$agora','$verifcode',
            0,0,'0','',0,'0','0')";
            //echo $sqlu; exit;
    $resultu=mysql_query($sqlu);
    header("location:message.php?msg=2"); exit;
}
?>

HTML

<tr>
 <td class="normal" align="right"><font class="a">*</font>&nbsp;Senha de acesso:&nbsp;</td>
 <td><input style="width:60%" type="password" name="pass_word" value="<?=$pass?>"/></td>
</tr>
<tr>
 <td class="normal" align="right"><font class="a">*</font>&nbsp;Confirma&ccedil;&atilde;o de senha:&nbsp;</td>
 <td><input style="width:60%" type="password" name="cpassword" value="<?=$pass?>"/></td>
</tr>
    <input type="submit" value="Atualizar cadastro" name="Editar" class="bttn" /></td>

Guys, I got a problem here and I couldn’t find a solution.

This is an admin panel, I implemented it to save the passwords in MD5 in DB.

There are two functions, one to execute when editing a current client and one to create a new client (salva_nv)...

The problem is in the function of editing the client (salva_ed), in the password fields.

When I edit it changes the password of the client generating another MD5.

How to make it check if the record is the same in the column, and not replace the current password?

When loading the page it already automatically fills the fields with the current password, but it shows in MD5, so if I save it will change in DB generating a new MD5 key.

I saw on the internet that it is possible to use "Constraint" to accomplish this task, but I could not implement.

Does anyone have any tips?

2 answers

1

You can leave a warning in the talking system to leave blank if you do not want to change the password and do so:

    if($_REQUEST['salva_ed']){
if(!empty($pass){$sqlu="update registration set username='$username', firstname='$fname',  lastname='$lname', sex='$gender',
        birth_date='$bdate', addressline1='$address1',addressline2='$address2', complemento='$address3', numero='$address4', city='$city',  state='$state',
        country='$country', postcode='$zipcode', phone='$phone', mobile_no='$mobileno', full_mobileno='$fullmobile',
        email='$email', password=MD5('$pass'), cpf='$cpf',rg='$rg'  where id='$id'";}else{$sqlu="update registration set username='$username', firstname='$fname',  lastname='$lname', sex='$gender',
        birth_date='$bdate', addressline1='$address1',addressline2='$address2', complemento='$address3', numero='$address4', city='$city',  state='$state',
        country='$country', postcode='$zipcode', phone='$phone', mobile_no='$mobileno', full_mobileno='$fullmobile',
        email='$email', cpf='$cpf',rg='$rg'  where id='$id'";}
$resultu=mysql_query($sqlu);
header("location:message.php?msg=3"); exit;}
  • I have another problem here, I updated the question can give me another tip? rsrs

0


I don’t know how to do with Constraint but I would do so:

if($_REQUEST['salva_ed'] && $pass && $pass != ''){
$sqlu="update registration set username='$username', firstname='$fname',  lastname='$lname', sex='$gender',
        birth_date='$bdate', addressline1='$address1',addressline2='$address2', complemento='$address3', numero='$address4', city='$city',  state='$state',
        country='$country', postcode='$zipcode', phone='$phone', mobile_no='$mobileno', full_mobileno='$fullmobile',
        email='$email', password=MD5('$pass'), cpf='$cpf',rg='$rg' where id='$id' AND password != '$pass'";
$resultu=mysql_query($sqlu);
header("location:message.php?msg=3"); exit;
}

This way in Where it checks if the password is different from what is being suggested (without converting it to md5)

  • It didn’t work here, I did as mentioned but he keeps saving the password... Every time I click the save button, it changes the value in "password" with a new MD5.

  • It worked here, I copied your code and pasted here, it has an error at the end, ta out of " I left this way it worked: Where id='$id' AND password != '$pass'"; Thank you very much man, didn’t think it would be so simple rsrs, Valew!

  • Oops, mark the answer as right then it was worth =]

  • I have another problem here, I updated the question can give me another tip? rsrs

  • Create another question, it is important to keep the subject attached to the title for future research.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.