Results 1 to 9 of 9

Thread: INSERT INTO issues with apostrophe

  1. #1
    chrislynch8's Avatar
    chrislynch8 is offline Sugar Community Member
    Join Date
    Oct 2007
    Location
    Cork, Ireland
    Posts
    747

    Default INSERT INTO issues with apostrophe

    Hi,

    I have a Select statement that is returning multiple rows. THe information that is returned is getting inserted into a new table for an external applcation to access.

    When values in the select statment contain apostrophe e.g. Chris O'Leary the created insert contain the following
    PHP Code:
    INSERT INTO table  (nameVALUES ('Chris O'Leary  
    See the issue - It inserts the data but the apostrophe is appearing as ' so the database field appears as Chris O'Leary - In sugar I see that it uses the following for apostrophe
    PHP Code:
    INSERT INTO table (nameVALUE ('Chris O\ 'Leary
    For some reason the PHP code is not including the third apostrophe they should have ('Chris O'Leary') and ('Chris O\ 'Leary')

    How do I acheive something like that is a logic hook? where my statment has something like teh following

    PHP Code:
    INSERT INTO table (nameVALUES ('$name'
    Where $name is returned in the select statment along with many other fields.

    Rgds
    Chris
    Linkedin Profile:Chris Lynch

    FDC IT Solutions
    FDC House
    Wellington Road
    Cork
    Ireland

  2. #2
    agcopley is offline Sugar Community Member
    Join Date
    Nov 2007
    Location
    Santiago, Chile
    Posts
    204

    Default Re: INSERT INTO issues with apostrophe

    Quote Originally Posted by chrislynch8 View Post
    Hi,

    I have a Select statement that is returning multiple rows. THe information that is returned is getting inserted into a new table for an external applcation to access.

    When values in the select statment contain apostrophe e.g. Chris O'Leary the created insert contain the following
    PHP Code:
    INSERT INTO table  (nameVALUES ('Chris O'Leary  
    See the issue - It inserts the data but the apostrophe is appearing as ' so the database field appears as Chris O'Leary - In sugar I see that it uses the following for apostrophe
    PHP Code:
    INSERT INTO table (nameVALUE ('Chris O\ 'Leary
    For some reason the PHP code is not including the third apostrophe they should have ('Chris O'Leary') and ('Chris O\ 'Leary')

    How do I acheive something like that is a logic hook? where my statment has something like teh following

    PHP Code:
    INSERT INTO table (nameVALUES ('$name'
    Where $name is returned in the select statment along with many other fields.

    Rgds
    Chris
    If one does a quick search in the manual you'll find a php function addslashes() that does this type of thing.

  3. #3
    chrislynch8's Avatar
    chrislynch8 is offline Sugar Community Member
    Join Date
    Oct 2007
    Location
    Cork, Ireland
    Posts
    747

    Default Re: INSERT INTO issues with apostrophe

    Hi,

    Thats doesn't work.

    So my select statement is returning multiple rows, using

    PHP Code:
    while ($row $this->bean->db->fetchByAssoc($results)
    {
    $name $row['name'];
    $address $row['address'}


    $query "Insert into table (name, address) VALUE ('$name','$address')";

    .
    .
    .

    I tried using the addslashes() for $name = addslashes($row['name']) but it stil went into my table as '

    Rgds
    Chris

    Rgds
    Chris
    Last edited by chrislynch8; 2010-03-02 at 04:42 PM.
    Linkedin Profile:Chris Lynch

    FDC IT Solutions
    FDC House
    Wellington Road
    Cork
    Ireland

  4. #4
    Angel's Avatar
    Angel is offline Sugar Community Member
    Join Date
    Jul 2005
    Location
    Los Angeles
    Posts
    4,813

    Default Re: INSERT INTO issues with apostrophe

    The escape character for SQL operations is, ironically, an apostrophe.

    Thus, if you want to insert an apostrophe, it has to be referenced in this manner: VALUES ('Chris O''Leary' )

    You should be able to easily address that via a string replace so as to replace the value of any of the columns from ' to '' before referencing it in your query.

    BTW, this affects SELECT queries as well, not just inserts.
    Regards,

    Angel Magaña
    Co-Author: Implementing SugarCRM 5.x (Packt Publishing -- Sept. 2010)
    Blog: http://cheleguanaco.blogspot.com.
    Twitter: @cheleguanaco.

    ________
    | Projects: |_____________________________________
    |
    | CandyWrapper (.NET Wrapper for SugarCRM SOAP API). Source now available on GitHub!
    | GoldMine to SugarCRM Express Conversion. Latest: 1.0.1.7 (Nov. 3, 2009)
    | CRM SkyDialer (Skype Integration). Latest: 1.0.2 (Feb. 17, 2010)
    | Round Robin Leads Assignment
    | Phone Number Formatter
    | CaseTwit (Twitter Integration)
    ______________________________________________

  5. #5
    agcopley is offline Sugar Community Member
    Join Date
    Nov 2007
    Location
    Santiago, Chile
    Posts
    204

    Default Re: INSERT INTO issues with apostrophe

    Quote Originally Posted by Angel View Post
    The escape character for SQL operations is, ironically, an apostrophe.

    Thus, if you want to insert an apostrophe, it has to be referenced in this manner: VALUES ('Chris O''Leary' )

    You should be able to easily address that via a string replace so as to replace the value of any of the columns from ' to '' before referencing it in your query.

    BTW, this affects SELECT queries as well, not just inserts.
    The general character for escape operations in mysql is '\'.

    addslashes() has and continues to work well for me :-).

    Unless one enables NO_BACKSLASH_ESCAPES as sql mode in your mysql db, that is

    Rgds
    Andrew

  6. #6
    Angel's Avatar
    Angel is offline Sugar Community Member
    Join Date
    Jul 2005
    Location
    Los Angeles
    Posts
    4,813

    Default Re: INSERT INTO issues with apostrophe

    Oops..thanks for that. Got caught up in the MSSQL world.
    Regards,

    Angel Magaña
    Co-Author: Implementing SugarCRM 5.x (Packt Publishing -- Sept. 2010)
    Blog: http://cheleguanaco.blogspot.com.
    Twitter: @cheleguanaco.

    ________
    | Projects: |_____________________________________
    |
    | CandyWrapper (.NET Wrapper for SugarCRM SOAP API). Source now available on GitHub!
    | GoldMine to SugarCRM Express Conversion. Latest: 1.0.1.7 (Nov. 3, 2009)
    | CRM SkyDialer (Skype Integration). Latest: 1.0.2 (Feb. 17, 2010)
    | Round Robin Leads Assignment
    | Phone Number Formatter
    | CaseTwit (Twitter Integration)
    ______________________________________________

  7. #7
    agcopley is offline Sugar Community Member
    Join Date
    Nov 2007
    Location
    Santiago, Chile
    Posts
    204

    Default Re: INSERT INTO issues with apostrophe

    Quote Originally Posted by Angel View Post
    Oops..thanks for that. Got caught up in the MSSQL world.
    Naughty boy!

  8. #8
    Angel's Avatar
    Angel is offline Sugar Community Member
    Join Date
    Jul 2005
    Location
    Los Angeles
    Posts
    4,813

    Default Re: INSERT INTO issues with apostrophe

    hahahahahahhahahahahahahhahah
    Regards,

    Angel Magaña
    Co-Author: Implementing SugarCRM 5.x (Packt Publishing -- Sept. 2010)
    Blog: http://cheleguanaco.blogspot.com.
    Twitter: @cheleguanaco.

    ________
    | Projects: |_____________________________________
    |
    | CandyWrapper (.NET Wrapper for SugarCRM SOAP API). Source now available on GitHub!
    | GoldMine to SugarCRM Express Conversion. Latest: 1.0.1.7 (Nov. 3, 2009)
    | CRM SkyDialer (Skype Integration). Latest: 1.0.2 (Feb. 17, 2010)
    | Round Robin Leads Assignment
    | Phone Number Formatter
    | CaseTwit (Twitter Integration)
    ______________________________________________

  9. #9
    chrislynch8's Avatar
    chrislynch8 is offline Sugar Community Member
    Join Date
    Oct 2007
    Location
    Cork, Ireland
    Posts
    747

    Default Re: INSERT INTO issues with apostrophe

    So how does the addslashes() work correctly for you in my case where should I be using it? Should I have it the way I posted above or should it be used elsewhere?

    $name = addslashes($name) and then use $name in the INSERT statement?

    Rgds
    Chris
    Linkedin Profile:Chris Lynch

    FDC IT Solutions
    FDC House
    Wellington Road
    Cork
    Ireland

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Apostrophe in a name and email compose
    By s0me1 in forum Developer Help
    Replies: 0
    Last Post: 2009-10-13, 01:24 AM
  2. Apostrophe in RSS headline (2.5.0)
    By jpshea in forum Feature Requests
    Replies: 0
    Last Post: 2005-02-25, 06:09 AM
  3. Account Name w/Apostrophe
    By udannlin in forum Help
    Replies: 2
    Last Post: 2004-08-12, 04:19 AM

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
  •