简体   繁体   中英

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', 'Brandon.braner@gmail.com', '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 

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 (?, ?, ?, ?)');

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 (?, ?, ?, ?)'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM