Error when inserting an '

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Error when inserting an '

    I have a comment block on a form on my page. I want to
    store the value of that form in a field in my database.
    Everything works fine until the user types an apostrophe
    in their comments. Such as "don't do this". This
    appostrophe messes up my sql statement to insert the the
    value in the database and causes an error. I am using a
    stored procedure for the insert. Here is the script to
    call the stored procedure:

    Dim SurveyResults
    SurveyResults = "AddSuperSurvey '"&Request("Name")
    &"', '"&Request("Rank")&"', '"&_
    Request("AFSC")&"', '"&Request("Phone")
    &"', '"&Request("PASCODE")&"', '"&_
    Request("Q1")&"', '"&Request("Q2")&"', '"&Request
    ("Q3")&"', '"&_
    Request("Q4")&"', '"&Request("Q5")&"', '"&Request
    ("Q6")&"', '"&_
    Request("Q6Other")&"', '"&Request("Q2Comment")
    &"', '"&_
    Request("Q3Comment")&"', '"&Request("Q4Comment")
    &"'"
    SurveyConnection.Execute(SurveyResults)

    Michael Ralph Guest

  2. Similar Questions and Discussions

    1. Please Help! Error inserting record...
      I am using ColdFusion MX 6.1 and am having issues with inserting a record into an access database. Any help on what is wrong? Error Executing...
    2. Please Help! Error when inserting record...
      I am using ColdFusion MX 6.1 and am having issues with inserting a record into an access database. Any help on what is wrong? Error Executing...
    3. Error inserting records
      I am getting this error: Microsoft OLE DB Provider for ODBC Drivers error '80040e14' Syntax error in INSERT INTO statement My code is as...
    4. Error when inserting record
      When trying to use the server behavior "Insert Record" I get the following error message: While executing onLOad in Insert Record.htm, the...
    5. Inserting Page error
      When we are joining chapters by doing the Document \ Insert Pages we get the following error. Wrong type parameter supplied to a PDS procedure It...
  3. #2

    Default Re: Error when inserting an '

    Ah, the faq of faqs. [url]www.aspfaq.com[/url]
    Search for "apostrophe"

    Ray at work

    "Michael Ralph" <ralph@141.com> wrote in message
    news:01c401c38cf2$0d2be2a0$a301280a@phx.gbl...
    > I have a comment block on a form on my page. I want to
    > store the value of that form in a field in my database.
    > Everything works fine until the user types an apostrophe
    > in their comments. Such as "don't do this". This
    > appostrophe messes up my sql statement to insert the the
    > value in the database and causes an error. I am using a
    > stored procedure for the insert. Here is the script to
    > call the stored procedure:
    >
    > Dim SurveyResults
    > SurveyResults = "AddSuperSurvey '"&Request("Name")
    > &"', '"&Request("Rank")&"', '"&_
    > Request("AFSC")&"', '"&Request("Phone")
    > &"', '"&Request("PASCODE")&"', '"&_
    > Request("Q1")&"', '"&Request("Q2")&"', '"&Request
    > ("Q3")&"', '"&_
    > Request("Q4")&"', '"&Request("Q5")&"', '"&Request
    > ("Q6")&"', '"&_
    > Request("Q6Other")&"', '"&Request("Q2Comment")
    > &"', '"&_
    > Request("Q3Comment")&"', '"&Request("Q4Comment")
    > &"'"
    > SurveyConnection.Execute(SurveyResults)
    >

    Ray at Guest

  4. #3

    Default Re: Error when inserting an '

    VFAQ.
    [url]http://www.aspfaq.com/2035[/url]




    "Michael Ralph" <ralph@141.com> wrote in message
    news:01c401c38cf2$0d2be2a0$a301280a@phx.gbl...
    > I have a comment block on a form on my page. I want to
    > store the value of that form in a field in my database.
    > Everything works fine until the user types an apostrophe
    > in their comments. Such as "don't do this". This
    > appostrophe messes up my sql statement to insert the the
    > value in the database and causes an error. I am using a
    > stored procedure for the insert. Here is the script to
    > call the stored procedure:
    >
    > Dim SurveyResults
    > SurveyResults = "AddSuperSurvey '"&Request("Name")
    > &"', '"&Request("Rank")&"', '"&_
    > Request("AFSC")&"', '"&Request("Phone")
    > &"', '"&Request("PASCODE")&"', '"&_
    > Request("Q1")&"', '"&Request("Q2")&"', '"&Request
    > ("Q3")&"', '"&_
    > Request("Q4")&"', '"&Request("Q5")&"', '"&Request
    > ("Q6")&"', '"&_
    > Request("Q6Other")&"', '"&Request("Q2Comment")
    > &"', '"&_
    > Request("Q3Comment")&"', '"&Request("Q4Comment")
    > &"'"
    > SurveyConnection.Execute(SurveyResults)
    >

    Aaron Bertrand - MVP Guest

  5. #4

    Default Re: Error when inserting an '

    this has been my solution for a year now:

    Basically I created a function to replace the request object


    function getStr(val)
    tmpStr = Trim(Request("val"))
    tmpStr = Replace(tmpStr, "'", "`")
    getStr = tmpStr
    end function

    You can obviously do more cleaning there if you want, like the Trimming i
    did etc.


    to call it:

    Just replace "Request" with "getStr" wherever you use it on ur page...

    Dim SurveyResults
    SurveyResults = "AddSuperSurvey '"&getStr("Name")
    &"', '"&getStr("Rank")&"', '"&_
    getStr("AFSC")&"', '"&getStr("Phone")
    &"', '"&getStr("PASCODE")&"', '"&_
    getStr("Q1")&"', '"&getStr("Q2")&"', '"&getStr
    ("Q3")&"', '"&_
    getStr("Q4")&"', '"&getStr("Q5")&"', '"&getStr
    ("Q6")&"', '"&_
    getStr("Q6Other")&"', '"&getStr("Q2Comment")
    &"', '"&_
    getStr("Q3Comment")&"', '"&getStr("Q4Comment")
    &"'"
    SurveyConnection.Execute(SurveyResults)




    "Michael Ralph" <ralph@141.com> wrote in message
    news:01c401c38cf2$0d2be2a0$a301280a@phx.gbl...
    > I have a comment block on a form on my page. I want to
    > store the value of that form in a field in my database.
    > Everything works fine until the user types an apostrophe
    > in their comments. Such as "don't do this". This
    > appostrophe messes up my sql statement to insert the the
    > value in the database and causes an error. I am using a
    > stored procedure for the insert. Here is the script to
    > call the stored procedure:
    >
    > Dim SurveyResults
    > SurveyResults = "AddSuperSurvey '"&Request("Name")
    > &"', '"&Request("Rank")&"', '"&_
    > Request("AFSC")&"', '"&Request("Phone")
    > &"', '"&Request("PASCODE")&"', '"&_
    > Request("Q1")&"', '"&Request("Q2")&"', '"&Request
    > ("Q3")&"', '"&_
    > Request("Q4")&"', '"&Request("Q5")&"', '"&Request
    > ("Q6")&"', '"&_
    > Request("Q6Other")&"', '"&Request("Q2Comment")
    > &"', '"&_
    > Request("Q3Comment")&"', '"&Request("Q4Comment")
    > &"'"
    > SurveyConnection.Execute(SurveyResults)
    >

    Hannibal Guest

  6. #5

    Default Re: Error when inserting an '

    > tmpStr = Replace(tmpStr, "'", "`")

    Well, this is an interesting idea, but some people might think that when
    their data is sent back to them, funny, I didn't put a backwards apostrophe
    in there. And if you just globally replace ` with ' on the way back out to
    avoid this problem, you might inadvertently replace *intentional*
    back-apostrophes.

    Using some token character to represent a "problem" character is going to
    lead to confusion somewhere down the line. Better to escape it, IMHO, then
    the data goes in and comes out *as intended*...

    A


    Aaron Bertrand - MVP Guest

  7. #6

    Default Re: Error when inserting an '

    it all also depends on how badly you want that apostrophe in there...

    you could always just UN-replace( "`" to "'") it when you output the data
    again... :o)

    "Hannibal" <Dominique@webadstudio.com> wrote in message
    news:OLYlXPPjDHA.1696@TK2MSFTNGP12.phx.gbl...
    > this has been my solution for a year now:
    >
    > Basically I created a function to replace the request object
    >
    >
    > function getStr(val)
    > tmpStr = Trim(Request("val"))
    > tmpStr = Replace(tmpStr, "'", "`")
    > getStr = tmpStr
    > end function
    >
    > You can obviously do more cleaning there if you want, like the Trimming i
    > did etc.
    >
    >
    > to call it:
    >
    > Just replace "Request" with "getStr" wherever you use it on ur page...
    >
    > Dim SurveyResults
    > SurveyResults = "AddSuperSurvey '"&getStr("Name")
    > &"', '"&getStr("Rank")&"', '"&_
    > getStr("AFSC")&"', '"&getStr("Phone")
    > &"', '"&getStr("PASCODE")&"', '"&_
    > getStr("Q1")&"', '"&getStr("Q2")&"', '"&getStr
    > ("Q3")&"', '"&_
    > getStr("Q4")&"', '"&getStr("Q5")&"', '"&getStr
    > ("Q6")&"', '"&_
    > getStr("Q6Other")&"', '"&getStr("Q2Comment")
    > &"', '"&_
    > getStr("Q3Comment")&"', '"&getStr("Q4Comment")
    > &"'"
    > SurveyConnection.Execute(SurveyResults)
    >
    >
    >
    >
    > "Michael Ralph" <ralph@141.com> wrote in message
    > news:01c401c38cf2$0d2be2a0$a301280a@phx.gbl...
    > > I have a comment block on a form on my page. I want to
    > > store the value of that form in a field in my database.
    > > Everything works fine until the user types an apostrophe
    > > in their comments. Such as "don't do this". This
    > > appostrophe messes up my sql statement to insert the the
    > > value in the database and causes an error. I am using a
    > > stored procedure for the insert. Here is the script to
    > > call the stored procedure:
    > >
    > > Dim SurveyResults
    > > SurveyResults = "AddSuperSurvey '"&Request("Name")
    > > &"', '"&Request("Rank")&"', '"&_
    > > Request("AFSC")&"', '"&Request("Phone")
    > > &"', '"&Request("PASCODE")&"', '"&_
    > > Request("Q1")&"', '"&Request("Q2")&"', '"&Request
    > > ("Q3")&"', '"&_
    > > Request("Q4")&"', '"&Request("Q5")&"', '"&Request
    > > ("Q6")&"', '"&_
    > > Request("Q6Other")&"', '"&Request("Q2Comment")
    > > &"', '"&_
    > > Request("Q3Comment")&"', '"&Request("Q4Comment")
    > > &"'"
    > > SurveyConnection.Execute(SurveyResults)
    > >
    >
    >

    Hannibal Guest

  8. #7

    Default Re: Error when inserting an '

    I personally wouldn't alter my users' data, well, except of course when
    people enter something like "<javascript...." and try to be wise-asses.

    Ray at work

    "Hannibal" <Dominique@webadstudio.com> wrote in message
    news:eTD3%23SPjDHA.3340@tk2msftngp13.phx.gbl...
    > it all also depends on how badly you want that apostrophe in there...
    >
    > you could always just UN-replace( "`" to "'") it when you output the data
    > again... :o)
    >

    Ray at Guest

  9. #8

    Default Re: Error when inserting an '

    > you could always just UN-replace( "`" to "'") it when you output the data
    > again... :o)
    Except if I wanted the data to look like this:

    "hello `nuff said"

    Now it looks like this:

    "hello 'nuff said"


    Aaron Bertrand - MVP Guest

  10. #9

    Default Re: Error when inserting an '

    On Tue, 7 Oct 2003 18:55:48 +0200, "Hannibal"
    <Dominique@webadstudio.com> wrote:
    >this has been my solution for a year now:
    >
    >Basically I created a function to replace the request object
    >function getStr(val)
    > tmpStr = Trim(Request("val"))
    > tmpStr = Replace(tmpStr, "'", "`")
    > getStr = tmpStr
    >end function
    >
    >You can obviously do more cleaning there if you want, like the Trimming i
    >did etc.
    Thanks for the indirect help. :)

    I do the same as a matter of course, also some checking for SQL
    injection, that sort of thing. But I haven't ever TRIMmed the string,
    and that's a good idea as well.

    Jeff
    Jeff Cochran Guest

  11. #10

    Default Re: Error when inserting an '

    On Tue, 7 Oct 2003 13:00:44 -0400, "Aaron Bertrand - MVP"
    <aaron@TRASHaspfaq.com> wrote:
    >> tmpStr = Replace(tmpStr, "'", "`")
    >
    >Well, this is an interesting idea, but some people might think that when
    >their data is sent back to them, funny, I didn't put a backwards apostrophe
    >in there. And if you just globally replace ` with ' on the way back out to
    >avoid this problem, you might inadvertently replace *intentional*
    >back-apostrophes.
    >
    >Using some token character to represent a "problem" character is going to
    >lead to confusion somewhere down the line. Better to escape it, IMHO, then
    >the data goes in and comes out *as intended*...
    I didn't see that in the original (didn't actually pay attention).
    I've always been doing:

    strTemp = replace(strTemp,"'","''")

    It's hard to read, but it replaces a single quote with two single
    quotes. So far I haven't had issues with that method, but I'm always
    open to suggestions on cleaning data before it hits the database.

    Jeff
    Jeff Cochran Guest

  12. #11

    Default Re: Error when inserting an '

    Hannibal's suggestion was to replace ' with `

    Personally, I don't think token values are very "safe"... either on
    conversion or translation back, there's the potential to muck up the
    content. Escaping is a much safer technique, and I always double up
    single-quotes, like you show here...



    > I didn't see that in the original (didn't actually pay attention).
    > I've always been doing:
    >
    > strTemp = replace(strTemp,"'","''")
    >
    > It's hard to read, but it replaces a single quote with two single
    > quotes. So far I haven't had issues with that method, but I'm always
    > open to suggestions on cleaning data before it hits the database.

    Aaron Bertrand - MVP Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139