0

im currently working on a webshop and im now busy working on a reset password function. People enter in their email and a sql query checks if the email exists. If it exists a random string is created and will be inserted in the database. Now the problem is that the query with the random string is successfull but the data isnt being updated. Hopefully someone can help me. The code that i am using:

 //Import database connection in variable $conn
require("dbh.inc.php");

//Load composer's autoloader
require 'vendor/autoload.php';

//Load variable with password
require 'credentials.php';

//Escape bullshit
$email = $conn->real_escape_string($_POST['email']);

//Make all data in $email lowercase
$email = strtolower($email);

//Put SQL query in var $sql
$sql = "SELECT user_Id FROM customers WHERE user_Email='$email'";

//Execute query and put results in var $data
$data = $conn->query($sql);

//If the email exists
if ($data->num_rows > 0) {

    //Random characters
    $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    //Shuffle all data in var $str
    $str = str_shuffle($str);

    //Select only the data after 40th position
    $str = substr($str,40);

    //The url that will be send to the user
    $url = "http://example.com/users/resetPassword.php?token=$str&email=$email";

    //Execute SQL query
    $query = $conn->query("INSERT customers SET user_ResetToken='$srt' WHERE user_Email='$email'");

My database setup

Luuk Kenselaar
  • 161
  • 1
  • 7
  • 2
    Do you want UPDATE instead of INSERT? (That SQL statement has a syntax error, should not be "successfull"...) – jarlh Feb 09 '18 at 08:52
  • your query should be something like this: $sql = "SELECT user_Id FROM customers WHERE user_Email='".$email."';"; same with _insert_ statement – Long Luong Feb 09 '18 at 09:01

1 Answers1

0

You need to update the data insead on inserting into it:

 $query = $conn->query("INSERT customers SET user_ResetToken='".$str."' WHERE user_Email='".$email."';");

replace INSERT with the UPDATE, like this.

$query = $conn->query("UPDATE customers SET user_ResetToken='".$str."' WHERE user_Email='".$email."';");

Update

Also change:

$url = "http://example.com/users/resetPassword.php?token=$str&email=$email";

to

$url = "http://example.com/users/resetPassword.php?token=".$str."&email=".$email;
Long Luong
  • 764
  • 2
  • 14
  • 28