testing some code in PHP,(beginner on PHP)
My webside folder contain 2 folders, one private folder and one public.
on the private I have 3 files PHP, autoload.php, database.php, and functions.php
on the public I have 1 file signin.php.
On my database.php I setup the connection to mySQL database and in the signin.php file I call the var $pdo to write the data, but looks like the $pdo var is unset.
//autoload.php
<?php
error_reporting(E_ALL);
require "../private/functions.php";
require "../private/database.php";
//database.php
<?php
$host = "127.0.0.1";
$user = "root";
$password = "";
$database = "nx_database";
header('Content-Type: application/json');
try{
$pdo = new PDO ("mysql:host=$host;dbname=$database", $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = array("connectiondb" => "OK");
echo json_encode($result);
}
catch(PDOException $e){
echo json_encode(['error' => "DB Connection Fail" . $e->getMessage()]);
exit();
}
//functions.php
//Is empty file
//signin file
<?php
require "../private/autoload.php";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$staff = $_POST['staff_ID'];
$email = $_POST['email'];
$pass = $_POST['password'];
$name = $_POST['Name'];
$cpt = $_POST['isCPT'];
$data = array("staff_ID" => $staff, "email" => $email,"pass" => $pass,"name" => $name,"isCPT" => $cpt);
echo json_encode($data);
$regex = '/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if (preg_match($regex,$email)){ // check if mail ok
if (is_numeric($staff)){// check if staff is numeric
echo("test");
$sql = "INSERT INTO users_nx (staff_ID,password,email,isCPT,Name, date) VALUES (?,?,?,?,?,?)";
$statement = $pdo->prepare($sql);
$statement ->execute([$staff,$pass,$email,$cpt,$name,$date]);
echo("ok");
}// check if staff is numeric
}// check if the mail ok
}
as you can see in the screenshot $pdo var is not exist but why? since I call require "../private/autoload.php"; I should be able to use $pdo var define on database.php