Database problem

I am very meticulous with code. I cut and paste it and replace parts of it. I have noticed some strange problems with database code. Take the folowing code

$result = mysql_query("SELECT background FROM list WHERE postnumber=$id");

if (!$result) {
    $stringg='Could not run query:' . mysql_error();
$row = mysql_fetch_row($result);


$bg = str_replace(chr(13), " ", $bg);
$bg = str_replace(chr(10), " ", $bg);

$bg="$bg add this text";

$result1 = mysql_query("UPDATE list SET background = '$bg' WHERE postnumber=$id");

it does not work. Yet I cut and paste it from elsewhere. Sometimes it does not work for inexplicable reasons. Can anyone spot a problem with the code?

Its basically a program to delete unecessary returns from the code. That side works fine. I just cannot save the results

Have a look at the mysql function REPLACE() function, enables you to update data fields inside your database, then you can replace all of them in your table in fell swoop.

[google]mysql REPLACE[/google]

EDIT Or this thread which shows how to replace win/*nix/mac line ends.

I have found the bug. It was the ’ character that caused this. It caused an error. When someones post included the words “I’ve” then it crashed because the apostrophe was seen as the end of the update string

The replace maybe a good idea… thanks

You need to properly prepare your data for use in the SQL query. Prepared statements or, at least, take a look at mysql_real_escape_string. :wink:

Thanks Anthony. That sounds like a very good idea