0

I know this question has been asked many times, and I have looked, followed instructions from suggestions, and have come up with zero solutions.

  1. Check to see if I had a different number of columns in my table vs my code/ No issues there
  2. Checked spelling on all columns and make sure they match/ No issues there
  3. Check columns and make sure they are in the same order/ No issues there
  4. Check to make sure database is connected/ No issues there
  5. Make sure that your id is listed as NULL/ No issues there

Kinda lost on this one, so I'm reaching out.

Heres my code:

DbOperations.php

    <?php

    class DbOperations{

        private $con;

        function __construct(){

            require_once dirname(__FILE__).'/DbConnect.php';

            $db = new DbConnect();

            $this->con = $db->connect();

        }

        /*CRUD -> C -> CREATE */

        public function createUser($firstname, $lastname, $usercountry, $userphone, $useremail, $userpassword){
            if($this->isUserExist($useremail)){
                return 0;
            }else{
                $password = md5($userpassword);
    //This is where the error is occurring          
$stmt = $this->con->prepare("INSERT INTO `users` (`id`, `firstName`, `lastName`, `userCountry`, `userPhone`, `userEmail`, 'userPassword') VALUES (NULL, ?, ?, ?, ?, ?, ?);");
                $stmt->bind_param("ssssss", $firstname, $lastname, $usercountry, $userphone, $useremail, $password);

                if($stmt->execute()){
                    return 1;
                }else{
                    return 2;
                }
            }
        }

        public function userLogin($useremail, $userpassword){
            $password = md5($userpassword);
            $stmt = $this->con->prepare("SELECT id FROM users WHERE userEmail = ? AND userPassword = ?");
            $stmt->bind_param("ss",$username,$password);
            $stmt->execute();
            $stmt->store_result();
            return $stmt->num_rows > 0;
        }

        public function getUserByEmail($useremail){
            $stmt = $this->con->prepare("SELECT * FROM users WHERE userEmail = ?");
            $stmt->bind_param("s",$username);
            $stmt->execute();
            return $stmt->get_result()->fetch_assoc();
        }


        private function isUserExist($useremail){
            $stmt = $this->con->prepare("SELECT id FROM users WHERE userEmail = ?");
            $stmt->bind_param("s", $useremail);
            $stmt->execute();
            $stmt->store_result();
            return $stmt->num_rows > 0;
        }

    }
  ?>

registerUser.php

<?php

require_once 'DbOperations.php';

$response = array();

if($_SERVER['REQUEST_METHOD']=='POST'){
    if(
        isset($_POST['firstName']) and
            isset($_POST['lastName']) and
                isset($_POST['userCountry']) and
                isset($_POST['userPhone']) and
                    isset($_POST['userEmail']) and
                        isset($_POST['userPassword']))
        {
        //operate the data further

        $db = new DbOperations();

        $result = $db->createUser(  $_POST['firstName'],
                                    $_POST['lastName'],
                                    $_POST['userCountry'],
                                    $_POST['userPhone'],
                                    $_POST['userEmail'],
                                    $_POST['userPassword']
                                );
        if($result == 1){
            $response['error'] = false;
            $response['message'] = "User registered successfully";
        }elseif($result == 2){
            $response['error'] = true;
            $response['message'] = "Some error occurred please try again";
        }elseif($result == 0){
            $response['error'] = true;
            $response['message'] = "It seems you are already registered, please choose a different email and username";
        }

    }else{
        $response['error'] = true;
        $response['message'] = "Required fields are missing";
    }
}else{
    $response['error'] = true;
    $response['message'] = "Invalid Request";
}

echo json_encode($response);

I appreciate all the help in advance!

  • 3
    Read this guide to [find out the actual error](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param). – El_Vanja Jan 25 '21 at 17:35
  • 1
    And please consider [properly encrypting your users' passwords](https://stackoverflow.com/questions/30279321/how-to-use-phps-password-hash-to-hash-and-verify-passwords) as `md5` is a weak protection (even its own [manual](https://www.php.net/manual/en/function.md5) advises against using it for this purpose, as it's built for speed). – El_Vanja Jan 25 '21 at 17:37
  • All the things you've listed are just guesswork essentially. Save yourself the time and get the real error message, and then you can focus on the real problem – ADyson Jan 25 '21 at 17:50
  • If I did have to guess though, I'd say your inserting NULL into the id field of the users table is a mistake. If that's an auto increment field then it can't be null, and you should not be trying to set its value. Simply omit that field from the query entirely and let the database do its job. (And if it's not an auto increment field then a) why not and b) why would you set a null ID??). Outputting the real error message will help to confirm/deny this, and/or reveal if you have any other problems – ADyson Jan 25 '21 at 17:52

0 Answers0