Data is not inserted in mysql / php

72

I want to change password in mysql Db. Here is userChangePassword.php file

<?php
require_once '../include/db_operations.php';

$response = array();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['username']) and isset($_POST['password'])) {
        $db = new DbOperations();
        if ($db->changePassword($_POST['username'], $_POST['password'])) {
            $response['error'] = false;
            $response['message'] = "Change Password Successfully";

            // go to dboperation

        } else {
            $response['error'] = true;
            $response['message'] = "Password not changed";
        }
    } else {
        $response['error'] = true;
        $response['message'] = "Fill all the feilds";
    }
} else {
    $response['error'] = true;
    $response['message'] = "Invalid Request";
}

echo json_encode($response);

This file is db_Operations.php contain function of changing password in database Method is working fine but password is not changed in database but message is displayed "successfully changed password"

<?php
class DbOperations
{
    private $con;
    function __construct()
    {
        require_once dirname(__FILE__) . '/db_connected.php';

        $db = new DbConnect();
        $this->con = $db->connect();
    }

    public function userLogin($username, $pass)
    {
        $password = $pass;
        $stm = $this->con->prepare("SELECT id FROM student WHERE username = ? AND password = ? ");
        $stm->bind_param("ss", $username, $password);
        $stm->execute();
        $stm->store_result();
        return $stm->num_rows > 0;
    }

    public function getUserByUserName($username)
    {
        $stm = $this->con->prepare("SELECT * FROM student WHERE username = ?");
        $stm->bind_param("s", $username);
        $stm->execute();
        return $stm->get_result()->fetch_assoc();
    }

    /*For insertion*/
    public function createUser($username, $pass, $email)
    {
        $password = $pass;
        $stm = $this->con->prepare("INSERT INTO `student` (`id`, `username`, `password`, `email`) VALUES (NULL, ?, ?, ?);");
        $stm->bind_param("sss", $username, $password, $email);
        if ($stm->execute()) {
            return true;
        } else {
            return false;
        }
    }

    // for change password

    public function changePassword($username, $pass)
    {
        $password = $pass;
        $stm = $this->con->prepare("UPDATE student SET password = ? WHERE username = ? ;");
        $stm->bind_param("ss", $username, $password);
        if ($stm->execute()) {
            return true;
        } else {
            return false;
        }
    }
}

Output is :

{"error":false,"message":"Change Password Successfully"}

All other method and queries are fine only changedPassword() method is not performing well.

These are the parameters used in database:

enter image description here

People are also looking for solutions to the problem: php - how do I pass on large datasets across different route requests - Laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.