Jump to content

SQL syntax error


j05hr

Recommended Posts

I'm making a CMS and got to the part where you can edit and add things from the database (it already reads stuff off the database)

 

I click the button to add the content and I get this error.

 

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 '}' at line 5

 

Where would I check for this as there is no '}' anywhere near line 5?

 

Here is the code that submits the form

 

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>

<?php
$menu_name = $_POST['menu_name'];
$position = $_POST['position'];
$visible = $_POST['visible'];

?>
<?php
$query = "INSERT INTO subjects (
			menu_name, position, visible
		) VALUES (
			'{$menu_name}', {$position}, {$visible} 
		}";
if (mysql_query($query, $connection)) {
	//Success!
	header("Location: content.php");
	exit;
} else {
// Display error message.
echo "<p>Subject creation failed</p>";
echo "<p>" . mysql_error() . "</p>";
}
?>

<?php mysql_close($connection); ?>

 

And here is the code with the form.

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>

<?php
$menu_name = $_POST['menu_name'];
$position = $_POST['position'];
$visible = $_POST['visible'];

?>
<?php
$query = "INSERT INTO subjects (
			menu_name, position, visible
		) VALUES (
			'{$menu_name}', {$position}, {$visible} 
		}";
if (mysql_query($query, $connection)) {
	//Success!
	header("Location: content.php");
	exit;
} else {
// Display error message.
echo "<p>Subject creation failed</p>";
echo "<p>" . mysql_error() . "</p>";
}
?>

<?php mysql_close($connection); ?>

Link to comment
https://forums.phpfreaks.com/topic/180032-sql-syntax-error/
Share on other sites

The issue(s) is with this query:

$query = "INSERT INTO subjects (
			menu_name, position, visible
		) VALUES (
			'{$menu_name}', {$position}, {$visible} 
		}";

Because you're not using arrays with string-based indexes (or anything involving objects) there's no need to use curly braces (It's not illegal, but it's not a good habit do unnecessary things). You're also trying to close the VALUES ( with }. Finally if position and visible aren't of some type of int field they should have quotes (but I'll leave them out since I'm not sure)

 

$query = "INSERT INTO subjects (
			menu_name, position, visible
		) VALUES (
			'$menu_name', $position, $visible 
		)";

Link to comment
https://forums.phpfreaks.com/topic/180032-sql-syntax-error/#findComment-949789
Share on other sites

your last brace in the statement should be a parenthesis.

$query = "INSERT INTO subjects (
            menu_name, position, visible
         ) VALUES (
            '{$menu_name}', {$position}, {$visible} 
         )";

 

and you might need quotes around position or visible if the'yre not integers

Link to comment
https://forums.phpfreaks.com/topic/180032-sql-syntax-error/#findComment-949790
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.