Inserting Data into a MS SQL Database from a PHP form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fredb
    New Member
    • Feb 2007
    • 10

    Inserting Data into a MS SQL Database from a PHP form

    I am trying to store a last name that contains an apostrophe (O'Merle). It appears that PHP is adding a '\' to the last name (O\'Merle). The back slash is causing a SQL error.

    The last name is entered from a PHP form and is retrieved using $_POST. Is there away to keep the apostrophe or single quote and get rid of the back slash?

    lastname = [ O\'Merle ]
    Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][SQL Native Client][SQL Server]Incorrect syntax near 'Merle'., SQL state 37000 in SQLExecDirect

    Thanks.
    Fred Bernstein
  • cassbiz
    New Member
    • Oct 2006
    • 202

    #2
    Originally posted by fredb
    I am trying to store a last name that contains an apostrophe (O'Merle). It appears that PHP is adding a '\' to the last name (O\'Merle). The back slash is causing a SQL error.

    The last name is entered from a PHP form and is retrieved using $_POST. Is there away to keep the apostrophe or single quote and get rid of the back slash?

    lastname = [ O\'Merle ]
    Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][SQL Native Client][SQL Server]Incorrect syntax near 'Merle'., SQL state 37000 in SQLExecDirect

    Thanks.
    Fred Bernstein

    You will need to use stripslashes() in order to get rid of the backslashes

    [php]
    echo stripslashes($r ow['lastname']);
    [/php]

    Comment

    • iburyak
      Recognized Expert Top Contributor
      • Nov 2006
      • 1016

      #3
      Originally posted by fredb
      I am trying to store a last name that contains an apostrophe (O'Merle). It appears that PHP is adding a '\' to the last name (O\'Merle). The back slash is causing a SQL error.

      The last name is entered from a PHP form and is retrieved using $_POST. Is there away to keep the apostrophe or single quote and get rid of the back slash?

      lastname = [ O\'Merle ]
      Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][SQL Native Client][SQL Server]Incorrect syntax near 'Merle'., SQL state 37000 in SQLExecDirect

      Thanks.
      Fred Bernstein
      If you'll double an apostrophe problem will go away. Not double quote.

      [PHP]O''Merle[/PHP]

      Comment

      • fredb
        New Member
        • Feb 2007
        • 10

        #4
        Originally posted by iburyak
        If you'll double an apostrophe problem will go away. Not double quote.

        [PHP]O''Merle[/PHP]
        The double single quote helped get the record into the SQL server database. The approach that I had to take is as follows:

        $lname = $_POST['lastname'];

        // Remove backslash
        $lastname = preg_replace('/[\\\]/','',$lname);
        // Add another apostrophe (single quote)
        $lastname = preg_replace('/[\']/','\'\'',$lastn ame);

        I appreciate your help. Take care.
        Fred

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          Sounds like a magic_quotes_gp c problem: this link discusses the issue, and has a very nice work around.

          Comment

          Working...