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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.