allows get right to my problem, the code I've written here doesn't email my database and that i cannot figue out why. Right now I'm simply attempting to get a handle on php and sql so there's no reason for this form apart from learning. This is actually the error i get(the very first sentence 'connected to database' comes from my if statement):

"Attached to databaseError: You possess an error inside your SQL syntax look into the manual that matches your MySQL server version for the best syntax to make use of near ''test' ('name') VALUES ('daniel')' at line 1"

The code I've may look just a little confusing as a lot of it comes from w3schools plus some comes from a buddy. I am unable to understand why this code is not working, I've attempted many versions from the syntax according to lots of articles I've discovered on the internet and on stackoverflow but none of them appear to operate. I'm afraid that maybe I'm not even connectec towards the database, although my if statement informs me otherwise, to ensure that might be a problem?

Hopefully if the will get solved this will clarify database connection and conntacting a database from the form in a single hit. Thanks ahead of time men and here's my code.


<form action="insert.php" method="post">
Name: <input type="text" name="namefield" />
<input type="submit" />

PHP (place.php)

$dbhost  = 'localhost';
$dbname  = 'carbon_db';
$dbuser  = 'username';
$dbpass  = 'password'; 

$con = mysql_connect($dbhost, $dbuser, $dbpass);

if($con == FALSE)
    echo 'Cannot connect to database' . mysql_error();
    echo 'Connected to database';

mysql_select_db($dbname, $con);

$sql="INSERT INTO 'test' ('name') 
VALUES ('$_POST[namefield]')";

if (!mysql_query($sql, $con))
    die('Error: ' . mysql_error());
echo "1 record added";


Drop the quotes round the table title or change these to back ticks:


$sql="INSERT INTO 'test' ('name') VALUES ('$_POST[namefield]')";


$sql="INSERT INTO test ('name') VALUES ('$_POST[namefield]')";


$sql="INSERT INTO `test` ('name') VALUES ('$_POST[namefield]')";

use ` rather than ' when refering to table/column names

$sql="INSERT INTO `test` (`name`) 
VALUES ('$_POST[namefield]')";

Alter the single quotes all around the table title and also the column title to backticks. Or eliminate them altogether.

$sql="INSERT INTO `test` (`name`)  VALUES ('{$_POST['namefield']}')";

Also, don't reference associative arrays ($_POST) directly inside a string without needing {} syntax or splitting up the string - what you have carried out there issues an E_NOTICE and really should be prevented.

Read this completely - you would be amazed what you could (and should not) legally do in PHP strings...

It's frequently better to use backticks for MySQL as like every other storage engines it's it's own reserved names and it is own reserved place practices.

try with

$sql = "INSERT INTO `test` (`name`) VALUES ('".$_POST['namefield']."')";

Take away the single quotes around your sql statement and replace back-tics (unsure even they're necessary):

$sql="INSERT INTO `test` ('name') 
VALUES ('$_POST[namefield]')";