2

我在主机鳄鱼上有一个网站。我可以连接我的 pdo 语句,但插入语句似乎不起作用。现在我已经定义了这些值,但我计划使用从前一页表单中的 $_POST 中提取的变量。

<?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 
4

2 回答 2

5

命令中的 SQL 语法prepare包含错误:

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

应该

qry = $dbh->prepare('INSERT INTO contact (Name, `Email Address`, Message, Date) VALUES (?, ?, ?, ?)');
于 2012-04-24T17:18:09.243 回答
2

你有一个语法错误。以下行

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

应该

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

更新:

您的列名电子邮件地址包含一个空格,通过使用正确的引号标识符来转义它,例如

INSERT INTO contact (Name, `Email Address`, Message, Date) VALUES (?, ?, ?, ?)'
于 2012-04-24T17:18:57.463 回答