PHP and MySQL and apostrophes

Discussion in 'PHP' started by asgsoft, Oct 15, 2009.

  1. #1
    Hey everyone,

    This problem has been baffling me for a little bit now. It's to do with MySQL and apostrophes.

    Currently I have:
    
    <?php
    include '../config.php';
    mysql_connect("$host", "$user", "$pass") or die(mysql_error()); 
    mysql_select_db("$dbname") or die(mysql_error());
    
    $title = $_POST['title']; 
    $news = $_POST['edited'];
    $news = str_replace("<br>", "<br />", $news);
    $date = date("D j M Y"); 
    
    mysql_query("INSERT INTO news (title, contents, date) VALUES ('$title', '$news', '$date')") or die(mysql_error());
    echo ("Done <a href='index.php'>Click Here</a> to go back to admin area")
    ?>
    PHP:
    and if any of the variables was to have an apostrophe (') then it generates an error on the query line.

    Does anyone have any ideas how I can sort this out?

    Thanks
     
    asgsoft, Oct 15, 2009 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    plog, Oct 15, 2009 IP
  3. HomeComputerGames

    HomeComputerGames Peon

    Messages:
    871
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    HomeComputerGames, Oct 15, 2009 IP
    asgsoft likes this.
  4. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #4
    thanks!

    that works great!

    thank you again :)
     
    asgsoft, Oct 15, 2009 IP
  5. jnelson563

    jnelson563 Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Do you use mysqli_real_escape_string to also prevent .php?id=3' from triggering an error?
     
    jnelson563, Oct 15, 2009 IP