Page 1 of 1

update sql error

Posted: Thu Jan 13, 2011 3:39 pm
by danwguy
I have a form that selects all the blogs a user has posted from the db, echos them onto the page with a radio button next to each one, then the user selects which one they want to edit and clicks submit. The next page has 3 text boxes that are populated with the current blog info from the db they are title, blurb, and blog. the user makes the edits they want and then hits submit and it updates the db with the new content from those boxes. My problem is I keep getting sytax errors, here is the code for the update page...

Code: Select all

<?php
$title = $_POST['title'];
$blurb = $_POST['blurb'];
$blog = $_POST['blog'];
$num = $_POST['number'];
$safetitle = str_replace("'", "~", $title);
$safeblurb = str_replace("'", "~", $blurb);
$safeblog = str_replace("'", "~", $blog);

$con = mysql_connect("login into here obviously.");
				if(!$con) {
									die("Could not Connect: " .mysql_error());
									}
										@mysql_select_db("blog_db") or die("Unable to select database " .mysql_error());
	$result = mysql_query("UPDATE blogs SET title=$safetitle WHERE blogID=$num") or die("title UPDATE error: " . mysql_error());
	$result2 = mysql_query("UPDATE blogs SET blurb=$safeblurb WHERE blogID=$num") or die("Blurb UPDATE error: " . mysql_error());
	$result3 = mysql_query("UPDATE blogs SET content=$safeblog WHERE blogID=$num") or die("Content UPDATE error: " . mysql_error());
?>
<html>
<body>
<?
$newquery = mysql_query("SELECT * FROM blogs WHERE blogID = '$num'") or die("Could not re-SELECT stuff: " .mysql_error());
while($row = mysql_fetch_array($newquery)) {
$newtitle = $row['title'];
$newblurb = $row['blurb'];
$newblog = $row['content'];
}
$snewtitle = str_replace("~", "'", $newtitle);
$snewblurb = str_replace("~", "'", $newblurb);
$snewblog = str_replace("~", "'", $newblog);
?>
<p align="center">Your blog has been updated.<br /> The new title is: <?php echo $snewtitle ?><br />Your new blurb is: <?php echo $snewblurb ?><br />And your new blog is: <?php echo $snewblog ?></p>
<a href="test-edit.php">Click</a> to edit another blog &nbsp;&nbsp;&nbsp | &nbsp;&nbsp;&nbsp <a href="/">Click</a> to go back to your home page. 
</body>
</html>
now let's say the original title that populates into the title box is "Womans World Longboard Championships" - without the quotes, I change it to say "Mens World Longboard Championships" - again without the quotes, and click submit. I keep getting this error... title UPDATE 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 'World Longboard Championships WHERE blogID=12' at line 1 ... it's cuttong off the "Mens" part of the title and giving me that error, I don't see what the problem is, could anyone please help me out here, it makes no sense to me, the code looks like it should work. Guess I should mention that $num is passed from page to page as the blogID number based on whichever radio box they check. Thank you for the help in advance.

Re: update sql error

Posted: Thu Jan 13, 2011 6:41 pm
by mikosiko

Code: Select all

        $result = mysql_query("UPDATE blogs SET title=$safetitle WHERE blogID=$num")
your fields title, blurb and content seems to be CHAR/VARCHAR/TEXT, therefore your variables should be enclosed in ' ... otherwise if you echo your query it will show something like this:

Code: Select all

UPDATE blogs SET title=Womans World Longboard Championships WHERE blogId=12
which is no a valid sentence

Re: update sql error

Posted: Thu Jan 13, 2011 7:33 pm
by danwguy
So the UPDATE query should look like this then???

Code: Select all

$result = mysql_query("UPDATE blogs SET title='$safetitle' WHERE blogID='$num'")



Re: update sql error

Posted: Fri Jan 14, 2011 10:34 am
by social_experiment
Yeah that's the correct way.

Re: update sql error

Posted: Fri Jan 14, 2011 12:43 pm
by mikosiko
social_experiment wrote:Yeah that's the correct way.
assuming that the blogID column is type text too..

Re: update sql error

Posted: Fri Jan 14, 2011 6:28 pm
by danwguy
No the blogID is int auto increment it's just there so I can easily call the blogs adn edit or show them, plus assign a picture to the blog, or folder of pictures to that particular blog using the blogID number.