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>
" . $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
" . $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