Can someone tell me why in the following code, in the 'try' section, this code works: (first line after 'try')
$conn = new PDO("mysql:host=$servername;dbname=$dbasename", $username, $password); even though I have not declared a variable named $dbname,
but if I change it to "$databasename", which I have declared, it doesn't work. I don't understand that.
<code>
<?php
$databasename = "removed";
$servername = "removed";
$username = "removed";
$password = "removed";
$id=$_POST[id];
$courseID=$_POST[courseID];
$coursename=$_POST[coursename];
$firstname=$_POST[firstname];
$lastname=$_POST[lastname];
$middlename=$_POST[middlename];
$grade=$_POST[grade];
$year=$_POST[year];
$sex=$_POST[sex];
echo "<br/>Connecting to Database.....<br/>";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbasename", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO $databasename (studentID, lastName, firstName, middleName, courseName, courseNumber, year, grade, gender)
VALUES ('$id','$lastname','$firstname','$middlename','$coursename','$courseID','$year','$grade', '$sex')";
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
</code>