I'm kinda new to PHP, so I'm still in the learning phase. I hope that I can get some good answers here and maybe someone has some tips on improvements.
I know things can be wrong, but again I'm still learning.
The thing I now try to make is a SIMPLE "Change password" form for my site.
<?php
include '../config.php';
$connection = mysqli_connect($servername, $username, $password, $dbname);
if ($connection->connect_error){
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
if(isset($_POST['submit']) && $_POST['submit'] = "submit"){
$username = mysql_real_escape_string($connection, $_POST['username']);
$password = md5($connection, $_POST['password']);
$newpassword = md5($connection, $_POST['newpassword']);
$confirmnewpassword = md5($connection, $_POST['confirmnewpassword']);
$result = mysql_query("SELECT password FROM users WHERE username='$username'");
if(!$result) {
echo "The username does not exist!";
}
else if($password != mysql_result($result, 0)){
echo "The password is not correct!";
}
if($newpassword === $confirmnewpassword) {
$sql = mysql_query("UPDATE users SET password = '$newpassword' WHERE username = '$username'");
}
if(!$sql) {
echo "Password has been changed!";
}else{
echo "Passwords do not match!";
}
}
?>
<form name="newprwd" action="" method="post">
username :<input type="text" name="username" value=""><br>
Passord :<input type="password" name="password" value=""><br>
Nytt passord :<input type="password" name="newpassword" value=""><br>
Bekreft Passord :<input type="password" name="confirmnewpassword" value=""><br>
<input type="submit" name="submit" value="Endre passord"><br>
</form>
This is the code I have in my change-pw.php file.
$servername = "*****";
$username = "****";
$password = "***";
$dbname = "***";
This is how I connect to the database with the config.php file. I don't show the server name here, but you get the picture of how I connect to it.
The thing I want is the form to get the password and username from the database and change it.
Yes, I know it is some norwegian words in here, but that's only for the echo's.
My problem:
When I write in a username, password, new password and confirm password I get the messages from if(!$result) and from if(!$sql) but it's won't changes the password. It says that the username does not exist and password has been changed.
Anyone see the problem that I can't see?
I am hoping for positive and negative comments on this script so that I can improve.
Thanks!