Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ryan champin
16,836 PointsPHP mySQL adding data to table
Im following the query a database tutorial but im trying to do the opposite....instead of querying the database....im trying to use the data from the post variables and insert the data into my table....heres my code...i have the data from the $post variable up top but heres my sql code
try{
$db = new PDO("mysql:host=localhost;dbname=form_data;port=8889","root","root");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
}catch(Exception $e){
echo "cant connect";
exit;
}
try{
$results = $db->exec("INSERT INTO data VALUES ($name, $email)");
echo "Query Success!";
}catch(Exception $e){
echo "Query Unsuccessful";
}
5 Answers
Kevin Korte
28,149 PointsYour exec I did find a bit odd, I build a demo site where I took two form input values, and put them into a MySQL table, and than retrieved the results of that table into a HTML table outputted to the browser. I had no concern for security, but I got it working, which is always the first start.
What I might try doing is bringing everything back down to it's bear bones.
I'd try cleaning up your first try catch block like so
try {
$db = new PDO("mysql:host=localhost;charset=utf8;dbname=form_data;port=8889","root","root");
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch(PDOException $e) {
echo $e->getMessage();
}
and than I'd try
try{
$results = $db->prepare("INSERT INTO data (name, email) VALUES ( '$name', '$email')");
$results->execute();
echo "Query Success!";
}catch(PDOException $e){
echo "Query Unsuccessful";
}
Where data (name, email) would be the names of the columns in your table. The variable $name and $email in the VALUES parenthesis would be assuming that you assigned the results of your $_POST results for that input to those variable names. Adjust accordingly.
Now, again...huge warning. This code is in no way ready for production. It's not secure, it's not checking to see if the variables are even set, etc. It would need a lot of attention before going live, but it should be the minimum to get you up and going. From there you can build out the security it needs.
There are some good lessons on PDO here if you haven't watched them yet. And article I found immensely helpful that most of this code is based around can be found here:
http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
Let me know how that goes for you.
ryan champin
16,836 Pointsi know im doing something really wrong here so any help would be great!
Kevin Korte
28,149 PointsWhat are you actually getting back? Are you getting back one of the errors you coded in, or are you getting a different error back? Maybe the white screen of death? Knowing what is actually returning will help narrow this down.
ryan champin
16,836 Pointsits returning this: Query Unsuccessful Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/mobileTest/contact.php:129) in /Applications/MAMP/htdocs/mobileTest/contact.php on line 137
ryan champin
16,836 Pointsso my error is popping up.....but I'm not sure if my PDO object exec method is the right method to use or if I'm even using it correctly