简单的MySQL代码没有将数据插入数据库?

时间:2013-05-04 06:05:15

标签: php mysql

我已经在这工作了几个小时,似乎无法让这几行代码工作。这是我第一次使用MySQL,所以请耐心等待。

<?php

$con = mysql_connect("localhost","usernamewitheld","passwordwithheld");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("splashpage");
mysql_query("INSERT INTO 'email' ('emailaddress') VALUES ('$_POST[email]');", $con);
mysql_close($con);

?>

代码执行没有来自apache的任何错误,但数据库保持不受影响。有什么想法吗?

编辑:表格结构如下:

Field   Type    Null    Key Default Extra
emailaddress    text    NO  MUL NULL     
timesubmitted   timestamp   NO      CURRENT_TIMESTAMP    

4 个答案:

答案 0 :(得分:2)

删除半冒号,在查询中使用bacticks而不是单引号。

mysql_query("INSERT INTO `email` (`emailaddress`) VALUES ('$_POST[email]')", $con)
or     
die(mysql_error());

注意: Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:0)

在列名称`

周围使用反引号'
mysql_query("INSERT INTO 'email' ('emailaddress') VALUES ('$_POST[email]');", $con);

应该是

mysql_query("INSERT INTO `email` (`emailaddress`) VALUES ('$_POST[email]')", $con);

答案 2 :(得分:0)

使用此功能。

<?php
mysql_query("INSERT INTO email (emailaddress) VALUES ('".$_POST[email]."')", $con);
mysql_close($con);
?>

答案 3 :(得分:0)

尝试

<?php

$con = mysql_connect("localhost","usernamewitheld","passwordwithheld");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("splashpage",$con);
mysql_query("INSERT INTO 'email' ('emailaddress') VALUES ('$_POST[email]');", $con);
 echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
mysql_close($con);

?>