SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    PHP Code:

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

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

    $bg=$row[0];


    $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?

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its basically a program to delete unecessary returns from the code. That side works fine. I just cannot save the results

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    mysql REPLACE

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

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    scarborough
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Anthony. That sounds like a very good idea


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •