how to insert string that contains " " and ' ' into database using php ?

Discussion in 'PHP' started by ramysarwat, Oct 29, 2010.

  1. #1
    i have a huge text array that contains " " and ' ' in some rows

    so how can i insert this data into database using php ?
     
    ramysarwat, Oct 29, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    You can wrap the
    addslashes()
    PHP:
    function around your text variable, example addslashes($text)

    You could also take a look at htmlspecialchars() — Convert special characters to HTML entities
     
    MyVodaFone, Oct 29, 2010 IP
  3. ramsarvan

    ramsarvan Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    hi,

    yes addslashes() will work,
    use it like this,

    $qry = mysql_query("Insert into tablename (field1,field2) values ('".addslashes($content1)."','".addslashes($content2)."')");
     
    ramsarvan, Oct 29, 2010 IP
  4. Nythe

    Nythe Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you're using MySQL, you could also use the MySQL function: mysql_real_escape_string() which will escape the following characters: \x00, \n, \r, \, ', " and \x1a. (php.net)
     
    Nythe, Oct 29, 2010 IP
  5. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Don't forget prepared statements.. Don't directly query the db.
     
    krsix, Oct 30, 2010 IP