0

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.

7
  • 1
    remove exit on this line echo 'data:::'.$_POST['firstname'];exit; Commented Jan 10, 2019 at 9:51
  • @MasivuyeCokile : Removed but same result. Commented Jan 10, 2019 at 9:52
  • 4
    check if there are any errors by turning on error_reporting and display_errors Commented Jan 10, 2019 at 9:57
  • Getting error <b>Fatal error</b>: Uncaught Error: Class 'Database' not found in /var/www/html/restapi/api/create_user.php:16. Commented Jan 10, 2019 at 10:03
  • Where's the constructor for the class Database? Commented Jan 10, 2019 at 10:07

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.