HELP! inserting an apostrophe on the database

Discussion in 'PHP' started by buknoii, Oct 14, 2007.

  1. #1
    hi there! i'm having a difficulting inserting a data on the database wherein data that is being entered has an apostrophe.

    how do you fix this one? my code is below

    
    
    $query = " insert into photos
    		(date, photo_url , photo_description) values
    		('$frm_date','$frm_photo_url','$frm_photo_description')"
    		;
    
    
    PHP:
    and everytime i run this one an error is directed on the $frm_photo_description

    how do i get by with the apostrophe?
     
    buknoii, Oct 14, 2007 IP
  2. jmhyer123

    jmhyer123 Peon

    Messages:
    542
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what is the exact error? I don't think it's the apostrophe that is messing you up as you have apostrophes placed before that but it doesn't give an error on those.

    EDIT
    -----
    I didn't get what you were saying but, thanks to Lordy below, I realize you are talking about the actual data that is set in the variable ;). Lordy is right, the alternative is making the field (in your DB) a text field or "blob" or something similar (can't remember exactly what it's called on SQL)
     
    jmhyer123, Oct 14, 2007 IP
  3. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #3
    your gonna need to insert it with an escape (\)

    best way would be to first explode $frm_photo_description based on '
    and then implode with \'
     
    Lordy, Oct 14, 2007 IP
  4. buknoii

    buknoii Guest

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hello im trying to put this text

    Whether it's for sale or as giveaways, City has enough for everybody. Vendors sell cheap vegetables on the road every afternoon at the Agdao Public Market.


    and it produces this error

    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 's for sale or as giveaways, City has enough for everybody. Vendors sell cheap ve' at line 3
     
    buknoii, Oct 14, 2007 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Instead of exploding and imploding as per Lordy's suggestion (which is assuming that the ONLY problems are with single quotes...) you should use the mysql_real_escape_string function.
     
    TwistMyArm, Oct 14, 2007 IP
  6. buknoii

    buknoii Guest

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks for the help

    anyways where do i put this code

    mysql_real_escape_string? and how do i put it?
     
    buknoii, Oct 14, 2007 IP
  7. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $query = " insert into photos
            (date, photo_url , photo_description) values
            ('$frm_date','$frm_photo_url',mysql_real_escape_string($frm_photo_description))";
    PHP:
     
    Lordy, Oct 14, 2007 IP
  8. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Try this:

    $query = " insert into photos
            (date, photo_url , photo_description) values
            ('$frm_date','$frm_photo_url','" . mysql_real_escape_string($frm_photo_description) . "')";
    PHP:
    When you want to redisplay the content use stripslashes()

    Brew
     
    Brewster, Oct 15, 2007 IP