0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help I've been going around in the circles for the past day trying to fix, just looked into PDO thanks for the suggestion!!

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.