简体   繁体   English

PHP PDO插入语句不起作用

[英]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. 我可以连接我的pdo语句,但是插入的语句似乎不起作用。 Right now I have defined the values but i plan to use variabled pulled from a $_POST from a form on the previous page. 现在,我已经定义了值,但是我计划使用从上一页中的$ _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 

The SQL syntax in your prepare command contains errors: prepare命令中的SQL语法包含错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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