0

The php page below is meant to update a table record, and when successful, it showed perform the following echo:

echo "<p class = 'updated' >The Report with the ID [$id] has been updated successfully, Great Job!</p><br>"  ,  '<a class = "button" href="sql_select_updated.php">Click to go back to Table of Reports</a>'`

however, when this pho page is returned, it comes all empty, i tried to debugged it and got an error of

"undefined variable sql in line 101" ,

i dont understand this error, any help ? thanks.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Update Report</title>
    <link rel="stylesheet" href="update.css">
    
</head>
<body>
    <?php
        
        //database connection variables for the database on your web server
        $servername = "localhost"; //you can leave sername as localhost, as the database is on the same server as apache. 
        $username = "*****";
        $password = "*****";
        $database = "attack_titan";

        //we start a try and catch block to attempt to connect to our database and run the query. If it fails, we see the error/exception generated by the catch
        
        try {
            if($_SERVER['REQUEST_METHOD'] == 'POST') //The update / submit button has been pressed
            {
                
                $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password); //building a new connection object
                // set the PDO error mode to exception
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                
                $id = $_POST['id']; //the ID of the person we wanted to edit from the hidden form field
                $report_status = $_POST['report_status']; // Report Status opt in from $_POST
               

                $sql = $conn->prepare("UPDATE it_report SET  report_status = ? WHERE id =?");
                $sql -> bindValue(1, "$report_status"); //we bind this variable to the first ? in the sql statement
                $sql -> bindValue(2, $id); //we bind this value to the second ? in the sql statement
                
                $sql -> execute(); //execute the statement

                echo "<p class = 'updated' >The Report with the ID [$id] has been updated successfully, Great Job!</p><br>"  ,  '<a class = "button" href="sql_select_table.php">Click to go back to Table of Reports</a>';
               

            }
            
            else{
                $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password); //building a new connection object
                // set the PDO error mode to exception
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $id = $_GET['id']; //the ID of the person we want to edit from the URL
                
                $sql = $conn->prepare("SELECT * FROM it_reports WHERE id = ?");
                $sql -> bindValue(1, $id); //we bind this variable to the first ? in the sql statement

                $sql -> execute(); //execute the statement

                $row = $sql->fetch();
                
                //********* Pre-populate drop down logic *********//
                /*Depending on the value from the database, we pre populate the selected value of 
                the newsletter drop down accordingly by echoing either selected = "selected" or
                an empty string*/

                if ($row['report_status'] == 'incomplete') {
                    $report_incomplete = 'selected = "selected"';
                }else{
                    $report_incomplete = '';
                }

                if ($row['report_status'] == 'complete') {
                    $report_complete = 'selected = "selected"';
                }else{
                    $report_complete = '';
                }
                if ($row['report_status'] == 'postponed') {
                    $report_postpone = 'selected = "selected"';
                }else{
                    $report_postpone = '';
                }

                
                //********* END Pre-populate drop down logic *********//

                //inf'ormation about the person
                echo "ID: ", $row['id'] . '<br><br>';
                echo "Staff Name: ", $row['staff_name'] . '<br><br>';
                echo "Email: ", $row['email'] . '<br><br>';
                echo "Subject: ", $row['subjects'] . '<br><br>';
                echo "Problem Type: ", $row['problem_type'] . '<br><br>';
                echo "Description: ", $row['descriptions'] . '<br><br>';
                //using an include to seperate the echo statement that handles the form
                //this allows us to seperate code so it is easier to manage
                include "update_report_form.php";

                echo '<hr><br>';
                
            }
        }
        catch(PDOException $e)
            {
            echo $sql . "<br>" . $e->getMessage(); //If we are not successful in connecting or running the query we will see an error
            }
        ?>


</body>
</html>
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Have you checked that the values $id and $report_status are being initialised? It sounds like one of them might be empty? –  Mar 07 '22 at 21:05
  • **undefined variable sql in line 101** says it all. Did you go to line 101 to check it out? – GetSet Mar 07 '22 at 21:08
  • @GetSet i did check it , it is this code { echo $sql . "
    " . $e->getMessage(); //If we are not successful in connecting or running the query we will see an error } and i cant see any issue in it
    – Blood Seeker Mar 07 '22 at 21:11
  • @Kendle how can i check if they have empty values ? – Blood Seeker Mar 07 '22 at 21:12
  • And from there did you locate where you define $sql? – GetSet Mar 07 '22 at 21:14
  • Clue: your exception is occuring somewhere in the try block before $sql get's defined, and thus your "catch" doesn't have it. – GetSet Mar 07 '22 at 21:18
  • @GetSet the $sql variable is defined there however as such : $sql = $conn->prepare("UPDATE it_report SET report_status = ? WHERE id =?"); – Blood Seeker Mar 07 '22 at 21:21
  • No, the $sql variable isn't defined, hence the undefined variable sql in line 101 – GetSet Mar 07 '22 at 21:21
  • 1
    Think about what @GetSet told you -- if an error occurs in a try - catch block, it jumps to the catch immediately. Remove the $sql variable from your exception and you should see what your actual error is in the exception message. Your try-catch blocks should be smaller -- and only around the specific database code you are trying to catch exceptions for. You shouldn't have a giant try catch around all that code. You should have several. Try making some functions as well so you aren't repeating the exact same db connection code – gview Mar 07 '22 at 21:27
  • @GetSet i have to submit this assignment at 11:59, its 11:30 :') help a brother out – Blood Seeker Mar 07 '22 at 21:36
  • Must have been some weekend – GetSet Mar 07 '22 at 21:38
  • @GetSet I have been working on this since 2 weeks, its an entire system .. i can't really see well now .. can't wait to finish this assignment and get some sleep. – Blood Seeker Mar 07 '22 at 21:40
  • 1
    `$sql` only exists within the try, not within the catch. They are different [scopes](https://www.php.net/manual/en/language.variables.scope.php). As GetSet said, remove the `$sql` from the catch. e.g. change `echo $sql . "
    " . $e->getMessage();` to just `echo $e->getMessage();` . Then when you get the exception, just focus on the error message to begin with, and try to resolve whatever the problem is.
    – ADyson Mar 07 '22 at 23:26
  • Reference guide: ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – ADyson Mar 07 '22 at 23:29
  • As @ADyson mentiond, you define `$sql` in try block in the if block in line 34 and in the else block in line 52. In catch block non of these is defines. If you don't want to remove the $sql from catch block you have to define $sql before the try block e.g. in line 19 as `$sql = "";` then it should work also. – h.m.i.13 Mar 08 '22 at 06:39
  • If `$sql is undefined`, look for the line with `$sql = ...` and debug it. – Rick James Mar 09 '22 at 16:50

0 Answers0