update sql error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

update sql error

Post 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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: update sql error

Post 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
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: update sql error

Post 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'")


User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: update sql error

Post by social_experiment »

Yeah that's the correct way.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: update sql error

Post by mikosiko »

social_experiment wrote:Yeah that's the correct way.
assuming that the blogID column is type text too..
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: update sql error

Post 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.
Post Reply