0

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

pdo

ADyson
  • 57,178
  • 14
  • 51
  • 63
Damiano Miazzi
  • 1,514
  • 1
  • 16
  • 38
  • 1
    Does it fail if you actually try to run the code? It _could_ just be an IDE issue – ADyson Oct 28 '22 at 14:53
  • P.S. As an aside, `echo json_encode(['error' => "DB Connection Fail" . $e->getMessage()]);` should _not_ be outputting the exception message, for security and usability reasons. You should be logging the exception message to a file on the server, and outputting something generic and bland to the client-side, so that you a) don't scare any users with technical jargon, and b) don't accidentally leak any interesting technical information to a malicious user who could use it to understand better how to attack your site. – ADyson Oct 28 '22 at 14:55
  • Do not pay attention to this screenshot. **Only pay attention the actual code you run**. *Does* it produce any errors? – Your Common Sense Oct 28 '22 at 14:59
  • @YourCommonSense re the duplicate... the IDE here looks like VS Code rather than PHPStorm, but I don't know...maybe the fix will be the same? – ADyson Oct 28 '22 at 15:05

0 Answers0