I created a Mysql database in XAMPP server and I now I wanted to insert data by postman to database. I did all that true but unexpected error occurred. It seems error is because of app.php of Slim.
Here is my index.php.
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../includes/dboperation.php';
$app = new \Slim\App;
$app->post('/createuser', function (Request $req, Response $res) {
if (IsEmpty(array('name', 'password'), $res)) {
$reqdata = $req->getParsedBody();
$name = $reqdata['name'];
$pass = $reqdata['password'];
$hashed_password = password_hash($pass, PASSWORD_DEFAULT);
$db = new DbOperation();
$result = $db->createUser($name, $hashed_password);
if ($result == USER_CREATED) {
$Message = array();
$Message['error'] = false;
$Message['msg'] = 'User had been created successfully !';
$response->write(json_encode($Message));
return $response->withHeader('Content-type', 'application/json')->withStatus(201);
} else if ($result == USER_FAIELD) {
$Message = array();
$Message['error'] = true;
$Message['msg'] = 'Problem occurred !';
$response->write(json_encode($Message));
return $response->withHeader('Content-type', 'application/json')->withStatus(422);
} else {
$Message = array();
$Message['error'] = true;
$Message['msg'] = 'Username is invalide !';
$response->write(json_encode($Message));
return $response->withHeader('Content-type', 'application/json')->withStatus();
}
}
});
function IsEmpty($required_param, $response)
{
$er = false;
$er_code = '';
$request_param = $_REQUEST;
foreach ($required_param as $param) {
if (!isset($request_param[$param]) || strlen($request_param[$param]) <= 0) {
$er = true;
$er_code .= $param . ', ';
$response->write(json_encode($er_code));
}
}
if ($er) {
$err = array();
$err['error'] = true;
$err['msg'] = 'Losted param : ' . $er_code;
$response->write(json_encode($err));
}
return $er;
}
$app->run();
?>
Here is dboperation.php
<?php
class DbOperations
{
private $con;
function __construct()
{
require_once dirname(__FILE__) . '/dbconnect.php';
$db = new DbConnect;
$this->con = $db->connect();
}
public function createUser($Name, $Password)
{
if (!$this->NameValidation($Name)) {
$stmt = $this->con->prepare("INSERT INTO users(name,password) VALUES(?,?)");
$stmt->bind_param("ss", $Name, $Password);
if ($stmt->execute()) {
return USER_CREATED;
} else {
return USER_FAILED;
}
} else {
return USER_EXISTED;
}
}
private function NameValidation($Name)
{
$stmt = $this->con->prepare("SELECT id FROM users WHERE name = ?");
$stmt->bind_param("s", $Name);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
}
?>
This is the error:
Warning: Use of undefined constant DB_HOST - assumed 'DB_HOST' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 2
Warning: Use of undefined constant DB_USER - assumed 'DB_USER' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 3
Warning: Use of undefined constant DB_PASS - assumed 'DB_PASS' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 4
Warning: Use of undefined constant DB_NAME - assumed 'DB_NAME' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 5
Warning: Use of undefined constant USER_CREATED - assumed 'USER_CREATED' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 7
Warning: Use of undefined constant USER_EXISTED - assumed 'USER_EXISTED' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 8
Warning: Use of undefined constant USER_FAILED - assumed 'USER_FAILED' (this will throw an Error in a future version of PHP) in E:\xamp\htdocs\test\includes\constant.php on line 9
Fatal error: Uncaught RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening <?php tag? in E:\xamp\htdocs\test\vendor\slim\slim\Slim\App.php:625 Stack trace: #0 E:\xamp\htdocs\test\vendor\slim\slim\Slim\App.php(333): Slim\App->finalize(Object(Slim\Http\Response)) #1 E:\xamp\htdocs\test\public\index.php(69): Slim\App->run() #2 {main} thrown in E:\xamp\htdocs\test\vendor\slim\slim\Slim\App.php on line 625
Actually when I pass the require parameters to insert I get nothing to show that means empty page has shown and when I don't pass them the error occur but I expected the response that i coded.
Thank you for read this carefully!
Two things.
Firstly, you haven't defined the Constants, which means warnings are generated in your log. Check your constants.php
and make sure the constants are actually defined.
Secondly, you probably have display_errors
on. Never do that. Set it off either in your ini file, or with ini_set('display_errors', false);
.
The fatal error is caused by Output already having started before you expected it to. By turning display_errors
off, your app should run (albeit you might still need to fix those constants!)
For the best error logging experience, set error_reporting
to -1
, turn display_errors
off, and set a custom error_log
. Then in the terminal, type tail -f /path/to/error_log
. Your notices, warnings and errors will now scroll past in real time, without distorting your web page's display.