Jump to content

MySQL update error


happy2learn

Recommended Posts

I'm using MySQL version 4.1.21 Standard on a webhosting service in Australia.

 

I have successfully set up a database online using phpMyAdmin version 2.8.2 and PHP version 4.4.4

 

I've been able to code pages to INSERT stuff and to SELECT stuff and I've successfully coded a page to display two fields in textareas, then save the edited stuff.  The page that I post the edit form to displays the results without a hitch.

 

However, when I try to UPDATE the table with the strings that have displayed the stuff, I get an error that I just cannot solve ...

 

Here is the code I'm using:

 

<?php

 

$page2edit = $_POST["page_id"];

$description = stripslashes($_POST["description"]);

$keywords = $_POST["keywords"];

 

echo "<p>The page to edit is " . $page2edit . "</p>";

echo "<p>Description:<br /><span style='color: red'>" . $description . "</span></p>";

echo "<p>Keywords:<br /><span style='color: green'>" . $keywords . "</span></p>";

 

$keywords = addslashes($keywords);

 

THEN THE CONNECTION CODE (not included here for security reasons, but it works everywhere I've put it)

 

$query = "UPDATE pages SET page_description=$description, page_keywords=$keywords WHERE  page_id=$page2edit";

echo "<p>The SQL query is:<br />" . $query . "</p>";

mysql_query($query);

if (mysql_affected_rows() == 1) {

print '<p>The entry has been updated!</p>';

} else {

print "<p>Could not update the entry because: <strong>" . mysql_error() . "</strong>.<br />The query was $query.</p>";

}

mysql_close($con);

?>

 

OK now have a look at what that produces ...

 

 

 

The page to edit is 1

 

Description:

A great homepage for anyone living in Australia's Blue Mountains

 

Keywords:

search engine blue mountains

 

The SQL query is:

UPDATE pages SET page_description=A great homepage for anyone living in Australia's Blue Mountains,page_keywords=search engine blue mountains WHERE page_id=1

 

[here's the error message stuff ...]

 

Could not update the entry because: 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 'great homepage for anyone living in Australia's Blue Mountains,page_keywords=sea' at line 1.

The query was UPDATE pages SET page_description=A great homepage for anyone living in Australia's Blue Mountains,page_keywords=search engine blue mountains WHERE page_id=1.

 

 

I've tried putting single quotes around the string values but that doesn't work either.

 

The table in the database uses page_id as an auto-incremented increment and page_description and page_keywords are two of the other fields.

 

I can use phpMyAdmin for editing the database, but I'd rather do it by code I've written.  No trouble with INSERT or SELECT queries - only with UPDATE.

 

I'd be most grateful if anyone can show me why this is not working.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/54948-mysql-update-error/
Share on other sites

Use mysql_real_escape_string() and then quote your variables before database insertion so that the final insert string looks more like this:

 

UPDATE pages SET page_description='A great homepage for anyone living in Australia\'s Blue Mountains',page_keywords='search engine blue mountains' WHERE page_id=1

 

Link to comment
https://forums.phpfreaks.com/topic/54948-mysql-update-error/#findComment-271744
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.