简体   繁体   English

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

[英]Simple MySQL code not inserting data into database?

I've been working at this for hours, and can't seem to get these few lines of code to work. 我已经在这工作了好几个小时,似乎无法让这几行代码工作。 This is the first time I've worked with MySQL, so bear with me. 这是我第一次使用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);

?>

The code executes without any errors from apache, but the database remains unaffected. 代码执行时没有来自apache的任何错误,但数据库保持不受影响。 Any ideas? 有任何想法吗?

EDIT: Table structure is as follows: 编辑:表结构如下:

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

Remove semi colon, use bacticks instead of single quotes in query. 删除半冒号,在查询中使用bacticks而不是单引号。

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

Note: Please, don't use mysql_* functions in new code . 注意: 请不要在新代码中使用mysql_*函数 They are no longer maintained and are officially deprecated . 它们不再维护, 并且已被正式弃用 See the red box ? 看到红色的盒子 Learn about prepared statements instead, and use PDO , or MySQLi - this article will help you decide which. 了解准备好的语句 ,并使用PDOMySQLi - 本文将帮助您确定哪些。 If you choose PDO, here is a good tutorial . 如果您选择PDO, 这是一个很好的教程

use backtick ` here around column name not ' 使用反引号`这里围绕列名而不是'

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

should be 应该

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

Use this. 用这个。

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

try 尝试

<?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);

?>

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

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