I'm new to php and I'm having some trouble with the following code:
$code = $_COOKIE['user'];
$db=mysql_connect ("XXX.XXX.XXX", "XXX", "XXX") or die ('I cannot connect to the database because: ' . mysql_error());
$mydb=mysql_select_db("XXX");
$sql="SELECT * FROM accounts WHERE UserCode = '$code'";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$current = $row['Userpassword'];
};
$toldcurr = $POST["oldpass"];
$pass1 = $_POST["newpass1"];
$pass2 = $_POST["newpass2"];
if ($current !== $toldcurr) {
header ('Location: changepasserror1.html');
};
if ($pass1 !== $pass2) {
header ('Location: changepasserror2.html');
};
$timywhimy = gmdate('Y-m-d H:i:s e');
$query1 = "UPDATE accounts SET Userpassword='$pass2' WHERE UserCode='$code'";
$query2 = "INSERT INTO accountupdate (`User`, `Change`, `From`, `To`, `Time`)
VALUES ('$code', 'Password', '$current', '$pass2', '$timywhimy')";
mysql_query($query1);
mysql_query($query2);
header ('Location: changepass.html');
The problem i'm having is the
if ($current !== $toldcurr) {
header ('Location: changepasserror1.html');
};
if ($pass1 !== $pass2) {
header ('Location: changepasserror2.html');
};
is not working. When I know that the two variables are not equal, it still continues the script and changes the data in the MySQL database.
Any help is appreciated.
Update
I've updated my code to
if ($current !== $toldcurr) {
header ('Location: changepasserror1.html');
exit();
}
if ($pass1 !== $pass2) {
header ('Location: changepasserror2.html');
exit();
}
Apart from that, the only changes are mysql_
to mysqli_
.
The problem i'm having is it's always sending me to changepasserror2.html
, even if $pass1
and $pass2
are identical.
Also, it never sends me to changepasserror1.html
, even when $current
and $toldcurr
are different.
What should happen is if $pass1
= $pass2
and $current
= $toldcurr
, then the database should be updated and the redirect to a success page. If one of those variable does not equal each other, then it will redirect to an error page and no update should happen to the database.