2

lets get straight to my problem, the code I have written here does not write to my database and I cannot figue out why. At the moment I am simply trying to get to grips with php and sql so there is no point to this form other than learning. Here is the error i am getting(the first sentence 'connected to database' is from my if statement):

"Connected to databaseError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test' ('name') VALUES ('daniel')' at line 1"

The code I have may look a little confusing as some of it is from w3schools and some is from a friend. I cannot figure out why this code isn't working, I have tried many variations of the syntax based on loads of articles I have found online and on stackoverflow but none seem to work. I fear that maybe I am not even connectec to the database, although my if statement tells me otherwise, so that could be a problem?

Hopefully if this gets solved this question will clarify database connection and writing to a database from a form in one hit. Thanks in advance guys and here's my code.

HTML

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

PHP (insert.php)

<?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();
}
else
{
    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";

mysql_close($con)
?>

5 Answers 5

6

Drop the quotes around the table name or change them to back ticks:

Change:

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

To:

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

Or

$sql="INSERT INTO `test` ('name') VALUES ('$_POST[namefield]')";
Sign up to request clarification or add additional context in comments.

1 Comment

Or use proper quotes `` - it's a backticks actually. SO uses them for code highliting, so can't show how it wokrs :)
4

It's often best to use backticks for MySQL as like any other storage engines it has it's own reserved names and it's own reserved insert practices.

try with

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

Comments

3

Change the single quotes surrounding the table name and the column name to backticks. Or get rid of them all together.

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

Also, don't reference associative arrays ($_POST) directly in a string without using {} syntax or breaking up the string - what you have done there issues an E_NOTICE and should be avoided.

Read this thoroughly - you'd be amazed what you can (and can't) legally do in PHP strings...

3 Comments

Perfect works like a charm, thanks a lot mate. Although I do have one problem now, I don't have backticks on my keyboard, think its because I using UK keyboard. Does anyone know how to get around this? or a shortcut to get them on my keyboard? " <- those quote didn't work anywhere.
My UK keyboard has backticks on the top left key - below escape, above tab, to the left of number '1'.
Ahh thank you, I found it. Well this has been an invaluable half hour. Thanks for your help mate.
2

try using ` instead of ' when refering to table/column names

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

Comments

1

Remove the single quotes around your sql statement and replace with back-tics (not sure even they are necessary):

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.