kyle_maddisson Posted November 30, 2014 Share Posted November 30, 2014 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> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 30, 2014 Share Posted November 30, 2014 In the query you need to use the table name, not the database name. $sql = "INSERT INTO table_name (studentID, lastName, firstName, middleName, courseName, courseNumber, year, grade, gender)VALUES ('$id','$lastname','$firstname','$middlename','$coursename','$courseID','$year','$grade', '$sex')"; $conn->exec($sql); Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 30, 2014 Share Posted November 30, 2014 I noticed you are getting a post id, if you set id as the primary and use autoincrement in mysql you don't even have to pass an id in the insert query Each new entry would get the next incremental id automatic. http://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 30, 2014 Share Posted November 30, 2014 $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. You can connect to MySQL without declaring a database. If you attempt to query a table without selecting a database MySQL will report a database not selected error. Quote Link to comment Share on other sites More sharing options...
kyle_maddisson Posted December 1, 2014 Author Share Posted December 1, 2014 In the query you need to use the table name, not the database name. $sql = "INSERT INTO table_name (studentID, lastName, firstName, middleName, courseName, courseNumber, year, grade, gender) VALUES ('$id','$lastname','$firstname','$middlename','$coursename','$courseID','$year','$grade', '$sex')"; $conn->exec($sql); First of all, thank you for your reply. Does that mean I need to make another variable and assign it the table name ($databasetable = database.table)? I assume you cannot connect to the table because I tried hard coding it there and would get access denied error. Once I added '.Students' to the assigned database name, it worked. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 1, 2014 Share Posted December 1, 2014 You can assign the table name to a variable or hard code it. If you are getting an Access Denied error then there is an issue with your database credentials for connecting to mysql Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.