php - Beginning wit PDO, first program doesn't work
I began learning PDO today. This is my first program actually, and I'm stuck in the first program.
Here's a simple form which adds a single value to a database. When I click submit button, it gives Could not execute because of (The errorInfo function returns nothing)
Database Structure
Column Type
id int unsigned auto_increment
name varchar(255)
fillvalues.html
<form action="fillvalues.php" method="post">
<input type="text" name="test">
<input type="submit" name="submit">
</form>
fillvalues.php
<?php
include 'dbconnector.php';
//fill values
var_dump($db);
$sql="INSERT INTO test('name') VALUES ('" . $_POST['test'] . "')";
$smt=$db->query($sql);
if(!$smt)
{
$ei = $db->errorInfo();
die('Could not execute because of') . $ei[2];
}
else
{
echo "Added";
}
var_dump($smt);
var_dump($sql);
?>
dbconnector.php
<?php
try
{
$db=new PDO("mysql:host=localhost;dbname=pdo",'root','');
}
catch(PDOException $pe)
{
die('Could not connect to the database') . $pe->getMessage();
}
?>
Any suggestions are welcome.
Answer
Solution:
you have syntax error in your query. you shouldn't quote column names with apostrophe:
Answer
Solution:
this is how your statement should be if your using