satya
satya

Reputation: 3560

Getting error while accessing the class functions using PHP

I am facing one issue. I have one db-config file and once I am including this file to my php page I am not getting my input data.

create_user.php:

header("Access-Control-Allow-Origin: http://localhost/restapi/");
header("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// files needed to connect to database
include_once 'config/database.php';
include_once 'objects/user.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
echo 'data:::'.$_POST['firstname'];exit;
// instantiate product object
$user = new User($db);

The above is my php file where I am including the database file.

config/databse.php:

<?php
// used to get mysql database connection
class Database{
    // specify your own database credentials
    private $host = "localhost";
    private $db_name = "users";
    private $username = "root";
    private $password = "**********";
    public $conn;
    // get the database connection
    public function getConnection(){
        $this->conn = null;
        try{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }
        return $this->conn;
    }
}
?>

After including the database file and calling the method when I am trying to print post param its coming blank. I am using PHP 7.

Upvotes: 0

Views: 49

Answers (1)

echmel
echmel

Reputation: 154

enter image description here

Your code is correct absolutely, I've tested it but it seems you miss a symbol in the file's name. Try require_once instead of include_once.

And don't use ?> at the end of PHP file

Upvotes: 1

Related Questions