Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

SQL query keeps failing

I have a folder "cookmeup" on a wamp server in the www folder. Inside it are two folders, "include" and "v1". "include: contains the "config.php", DbConnect.php" and "DbOperations.php" files and "v1" contains the "registerUser.php" file

The Database on the wamp server is also called "cookmeup".

My issue is that everytime I use PostMan to check my PHP script, I always get a failed response. Here is the code

config.php

<?php
    define('DB_NAME','cookmeup');
    define('DB_USER','root');
    define('DB_PASSWORD','');
    define('DB_HOST','localhost');
?>

DbConnect.php

<?php
class DbConnect {
    private $con;

    function __construct(){

    }
    function connect(){
        include_once dirname(__FILE__).'/config.php';
        $this->con= new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

        if(mysqli_connect_error()){
            echo "Failed to connect to Database. Error ".mysqli_connect_errno;
        }
        return $this->con;
    }
}
?>

DbOperations.php

<?php

class DbOperations{
    private $con;
    function __construct(){
        require_once dirname(__FILE__).'/DbConnect.php';
        $db=new DbConnect();
        $this->con=$db->connect();
    } 

    //First Operation is Insertion
    function createUser($username,$email,$pass){
        $password=md5($pass);
        $stmt=$this->con->prepare("INSERT INTO `users` (`id`, `username`, `email`, `password`) VALUES (NULL,?,?,?);");
        $stmt->bind_param("sss",$username,$password,$email);

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

registerUser.php

<?php

    require_once '../include/DbOperations.php';

    $response = array();
    if($_SERVER['REQUEST_METHOD']=='POST'){
        if(isset($_POST['username']) and isset($_POST['email']) and isset($_POST['password'])){
            $db= new DbOperations();

            if($db->createUser($_POST['username'],$_POST['password'],$_POST['email'])){
                $response['error']=false;
                $response['message']="User registered successfully";
            }
            else{

                $response['error']=true;
                $response['message']="Some error occurred. 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);
?>

I just cannot figure out why my registerUser.php keeps failing to send a query. The result I get is always {"error":true,"message":"Some error occurred. Please try again"}

I have been following this to the letter with no success

Comments