'When I try and change the password to a test user account in my SQL Database it always returns "No database record matched with your data."
When I try and change the password to a test user account in my SQL Login Database it always returns "No database record matched with your data".
This is the main php file that changes the users password after a form has been filed with the old password and the new password, the actual form is working however I think the issue is that the database is not reading the email value correctly.
<?php
session_start();
$email = "[email protected]";
$valid = true;
$error = [];
$form_data = [];
if(!empty($_POST['old_password']))
{
$old_password = $_POST['old_password'];
$old_password_data = array("old_password" => $old_password);
$form_data = array_merge($form_data, $old_password_data);
$old_password_error = array("old_password_error" => "");
$error = array_merge($error, $old_password_error);
}
else
{
$valid = false;
$old_password = "";
$old_password_data = array("old_password" => $old_password);
$form_data = array_merge($form_data, $old_password_data);
$old_password_error = array("old_password_error" => "* Old password is required.");
$error = array_merge($error, $old_password_error);
}
if(!empty($_POST['new_password']))
{
$new_password = $_POST['new_password'];
$new_password_data = array("new_password" => $new_password);
$form_data = array_merge($form_data, $new_password_data);
$new_password_error = array("new_password_error" => "");
$error = array_merge($error, $new_password_error);
}
else
{
$valid = false;
$new_password = "";
$new_password_data = array("new_password" => $new_password);
$form_data = array_merge($form_data, $new_password_data);
$new_password_error = array("new_password_error" => "* New password is required.");
$error = array_merge($error, $new_password_error);
}
if(!empty($_POST['confirm_password']))
{
$confirm_password = $_POST['confirm_password'];
$confirm_password_data = array("confirm_password" => $confirm_password);
$form_data = array_merge($form_data, $confirm_password_data);
$confirm_password_error = array("confirm_password_error" => "");
$error = array_merge($error, $confirm_password_error);
}
else
{
$valid = false;
$confirm_password = "";
$confirm_password_data = array("confirm_password" => $confirm_password);
$form_data = array_merge($form_data, $confirm_password_data);
$confirm_password_error = array("confirm_password_error" => "* Confirm password is required.");
$error = array_merge($error, $confirm_password_error);
}
if($new_password != '' && $confirm_password != '')
{
if($new_password != $confirm_password)
{
$valid = false;
$confirm_password_error = array("confirm_password_error" => "* Confirm password is same as new password.");
$error = array_merge($error, $confirm_password_error);
}
if($new_password == $confirm_password)
{
$confirm_password_error = array("confirm_password_error" => "");
$error = array_merge($error, $confirm_password_error);
}
}
if($valid==true)
{
include 'db.php';
mysqli_select_db($con, 'users');
$check_data = "SELECT * FROM users WHERE email = $email";
$check_query = mysqli_query($con, $check_data);
$numRows = mysqli_num_rows($check_query);
$user_data = mysqli_fetch_assoc($check_query);
if($numRows == 1)
{
$check_old_password = password_verify($old_password,$user_data['password']);
if($check_old_password)
{
$new_password_encrypt = password_hash($new_password,PASSWORD_DEFAULT);
$user_id = $user_data['id'];
$sql = "UPDATE user_login SET password = '$new_password_encrypt' WHERE id = '$user_id' ";
$query = mysqli_query($con, $sql);
$row = mysqli_affected_rows($query);
if($row == 1)
{
echo "Your password successfully changed.";
die;
}
}
else
{
echo "Opps, We can not find your data. Please try again.";
die;
}
}
else
{
echo "No database record matched with your data.";
die;
}
}
else
{
$_SESSION['error'] = $error;
$_SESSION['form_data'] = $form_data;
header('Location:index.php');
}
?>
This is the database/database file that the main file is reading from which I can't see any issues with as the db.php file links up with all of the constants etc that I have used within the main php file.
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'users');
$con = mysqli_connect("localhost","root","","users");
/* Attempts to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Checks connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
