James
James

Reputation: 15

PHP script, SQL insert error

I'm running a wamp server and created a series of html webpages the data being put in by the user is then being submitted to a SQL database through a PHP script. I created a database table with Id. set to INT auto increment Crime. and Suspect. set as VARCHAR(40). I can connect to the database but now when I try to insert I get the error:

'Problem with insert: 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 'case (Id, Case, Suspect) VALUES('NULL', 'test', 'test12')' at line 1'

So I know its an issue with the insert function however cannot fix it!

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'fid';

$case = (isset($_POST['case'])? $_POST['case']:"None");
$suspect= (isset($_POST['suspect'])? $_POST['suspect']:"None");

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$conn)
die('Could not connect: ' . mysql_error());
mysql_select_db($db);
mysql_query("INSERT INTO case 
(Id, Case, Suspect) VALUES('NULL', '$case', '$suspect') ") 
or die(' Problem with insert: ' . mysql_error());  
echo 'Connected successfully';
mysql_close($conn);
?>

Upvotes: 0

Views: 76

Answers (1)

John Conde
John Conde

Reputation: 219934

case is a reserved keyword. If you're going to have a table and columnj named case you need to put it in ticks:

INSERT INTO `case` (Id, `Case`, Suspect) VALUES('NULL', '$case', '$suspect')

Also, 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.

Upvotes: 2

Related Questions