-1

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);
?>
  • 2
    Can't be explained with the shown excerpts alone. Albeit `unset($pdo);` hints at casual misuse somewhere in between. See also [Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – mario Feb 03 '19 at 07:07
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Progman Feb 03 '19 at 10:42
  • One more thing: `require_once` also just loads the referenced script ***once***; wherever it was called first. – mario Feb 04 '19 at 00:59

1 Answers1

0

Please check this code. You did not prepare and execute your code.

<?php
error_reporting(E_ALL);
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->prepare($data_det);
    $result_det->execute(['id'=>$id]);
    $result_det->setFetchMode(PDO::FETCH_ASSOC);
    if($result_det->rowCount()){

        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);
?>
Mizanur Rahman Khan
  • 1,612
  • 1
  • 12
  • 19