Brandon Braner
Brandon Braner

Reputation: 93

php pdo insert statement not working

I have a site on host gator. I can connect with my pdo statement but the statement for the insert doesnt seem to work. Right now I have defined the values but i plan to use variabled pulled from a $_POST from a form on the previous page.

<?php

/*** mysql hostname ***/
$hostname = 'xxx.xxx.xxx.xxx';

/*** mysql username ***/
$username = 'pressgym_admin';

/*** mysql password ***/
$password = '*******';  <-started out on purpose

try {
$dbh = new PDO("mysql:host=$hostname;dbname=pressgym_press", $username, $password);
/*** echo a message saying we have connected ***/
$qry = $dbh->prepare('INSERT INTO contact (Name, Email Address, Message, Date) VALUES (?, ?, ?, ?');
$qry->execute(array('Brandon', '[email protected]', 'test message', '3.12.12'));

echo 'entry succesfull';

}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

   describe contact;
   Name varchar(255)    NO  PRI     
   EmailAddress varchar(255)    NO          
   Message  longtext    NO          
   Date varchar(255)    YES 

Upvotes: 2

Views: 4473

Answers (2)

Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25753

you have a syntax error. the following line

$qry = $dbh->prepare('INSERT INTO contact (Name, Email Address, Message, Date) VALUES(?, ?, ?), ?');

should be

$qry = $dbh->prepare('INSERT INTO contact (Name, Email Address, Message, Date) VALUES (?, ?, ?, ?)');

Update:

your column name Email Address contains a space escape it by using proper quote identifier like

INSERT INTO contact (Name, `Email Address`, Message, Date) VALUES (?, ?, ?, ?)'

Upvotes: 2

eggyal
eggyal

Reputation: 125995

The SQL syntax in your prepare command contains errors:

qry = $dbh->prepare('INSERT INTO contact (Name, Email Address, Message, Date) VALUES (?, ?, ?), ?');

should be

qry = $dbh->prepare('INSERT INTO contact (Name, `Email Address`, Message, Date) VALUES (?, ?, ?, ?)');

Upvotes: 5

Related Questions