0

I have this error that I cannot fix when running my code.. I have looked online for solutions but nothing, I have no idea how to fix this.

<?php

class DbOperations
{
    private $con;

    function _construct()
    {
        require_once dirname(_FILE_).'/DBConnect.php';

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

    }

    /* CRUD - Create */
    function createUser($name, $surname, $student_number, $pass)
    {

        $password = md5($pass);

        // *** The error occurs here ***
        $stmt = $this->con->prepare("INSERT INTO `users` (`id`, `name`, `surname`, `student_number`, `password`) VALUES (NULL, ?, ?, ?, ?);");
        $stmt->bind_param("ssss",$name,$surname,$student_number,$password);

        if($stmt->execute())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

And then my registerUser.php

<?php

require_once '../include/DbOperations.php';
$response = array();

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if(
        isset($_POST['name']) and 
            isset($_POST['surname']) and
                isset($_POST['student_number']) and
                    isset($_POST['password']))
    {
        //Continue with using data
        $db = new DbOperations();

        if($db->createUser($_POST['name'],$_POST['surname'],$_POST['student_number'],$_POST['password']))
        {
            $response['error'] = false;
            $response['message'] = "User registered successfully";
        }
        else
        {
            $response['error'] = true;
            $response['message'] = "Some error occured, please try again.";
        }

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

    }

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

echo json_encode($response);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Clive
  • 1
  • 3
    Please dont __roll your own__ password hashing. PHP provides [`password_hash()`](http://php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://php.net/manual/en/function.password-verify.php) please use them. And here are some [good ideas about passwords](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet) If you are using a PHP version prior to 5.5 [there is a compatibility pack available here](https://github.com/ircmaxell/password_compat) – RiggsFolly Apr 13 '17 at 11:10
  • I did not go beyond `$password = md5($pass);` as you should really take care of this [security matter](http://stackoverflow.com/questions/14752416/php-md5-sha-hashing-security). please consider native PHP `password_hash()` and `password_verify()` EDIT: (note to myself: next time, try to get there faster than RiggsFolly ^^) – OldPadawan Apr 13 '17 at 11:11
  • 2
    Looks to me like your connection to the database failed! Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you can see on the screen. – RiggsFolly Apr 13 '17 at 11:13
  • A look at your PHP Error log may also have given you some clues – RiggsFolly Apr 13 '17 at 11:15
  • Your class constructor method is missing an underscore. `_construct()` should be `__construct()`. – Kirk Beard Apr 15 '17 at 19:23

0 Answers0