954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

"You have an error in your SQL syntax..."

I always get this error:
"Error updating joke: 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 '1' at line 1"

There's something about this part of my code that causes that error and I just can't figure out what that is. help please :(

$id = mysql_real_escape_string($_POST['id']);
	$text = mysql_real_escape_string($_POST['text']);
	$sql = mysql_query("UPDATE joke SET joketext='$text' WHERE id=$id");
kira_
Newbie Poster
9 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Make sure that the field names are correct. Anyway, post the codes above of these three lines. It may easy to find the problem quickly.

Zero13
Practically a Master Poster
624 posts since Jan 2009
Reputation Points: 120
Solved Threads: 139
 

try:

$sql = mysql_query("UPDATE joke SET joketext='$text' WHERE id='$id'") or die( mysql_error() );


notice the apostrophes around the value of $id

hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 

I always get this error: "Error updating joke: 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 '1' at line 1"

There's something about this part of my code that causes that error and I just can't figure out what that is. help please :(

$id = mysql_real_escape_string($_POST['id']);
	$text = mysql_real_escape_string($_POST['text']);
	$sql = mysql_query("UPDATE joke SET joketext='$text' WHERE id=$id");


comment the above codes and replace with this and post its output

$id = mysql_real_escape_string($_POST['id']);
$text = mysql_real_escape_string($_POST['text']);
$sql = mysql_query("UPDATE joke SET joketext='$text' WHERE id=$id");
print_r($_POST);
echo "";
echo $sql;
die();
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

I tre your code the only problem is the connection identifier that you didn't add in mysql_query. this is my code;

<?php
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("joking", $con);


$id = mysql_real_escape_string($_POST['id']);	
$text = mysql_real_escape_string($_POST['text']);	

$pre_sql = "UPDATE joke SET joketext='$text' WHERE id=$id";
$sql = mysql_query($pre_sql, $con);	

?>

i called the database joking ;)

faroukmuhammad
Junior Poster
140 posts since Sep 2008
Reputation Points: 20
Solved Threads: 16
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: