I got stuck for hours in the code below. I don't know how I can fix this error.
Notice: Undefined variable: pdo in C:\xampp\htdocs\latihan2\update.php on line 192 Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\latihan2\update.php:192 Stack trace: #0 {main} thrown in C:\xampp\htdocs\latihan2\update.php on line 192
**//file config.php**
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'demo2');
/* Attempt to connect to MySQL database */
try{
$pdo = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USERNAME, DB_PASSWORD);
// Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage());
}
?>
**//file update.php line 186-236**
<?php
require_once "config.php";
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
$id = trim($_GET["id"]);
$data_det = "SELECT * FROM employees_det WHERE id = :id";
$result_det = $pdo->query($data_det);
if($result_det->rowCount() > 0){
echo "<div class='table-responsive'>";
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID Parent</th>";
echo "<th>ID 2</th>";
echo "<th>Name</th>";
echo "<th>Job Level</th>";
echo "<th>Address</th>";
echo "<th>Salary</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result_det->fetch()){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['id2'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['level'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
// Free result set
unset($result_det);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
}
// Close connection
unset($pdo);
?>