0

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.

<?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. Any ideas?

EDIT: Table structure is as follows:

Field   Type    Null    Key Default Extra
emailaddress    text    NO  MUL NULL     
timesubmitted   timestamp   NO      CURRENT_TIMESTAMP    
Austin Berke
  • 87
  • 1
  • 7
  • could you please post table structure as well ? – Satya May 04 '13 at 06:06
  • 2
    [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 May 04 '13 at 06:08

4 Answers4

2

Remove semi colon, use bacticks instead of single quotes in query.

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

Note: Please, don't use mysql_* functions in new code. 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. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
0

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);
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

Use this.

<?php
mysql_query("INSERT INTO email (emailaddress) VALUES ('".$_POST[email]."')", $con);
mysql_close($con);
?>
Bharat Chodvadiya
  • 1,644
  • 4
  • 20
  • 31
0

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

?>
themhz
  • 8,335
  • 21
  • 84
  • 109