insertion into database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gudipati
    New Member
    • Oct 2007
    • 21

    insertion into database

    hi i have one checkbox in the form if i check that it should be check in the database field.(in database yes/no field for ckeckbox).what is my problem is every thing is stored except checkbox field.
    can u pl correct me?
    this is my check box.*******
    <input name="current" type="checkbox" id="current" value="">

    this is insertion code******
    [code=asp]
    <%

    If( (Request("SUBMI T") <> "") and(Request("SU BMIT") = "SUBMIT") ) Then
    set rstinsert = server.CreateOb ject("ADODB.Rec ordset")

    if Request("radiob utton") = "Ms" then
    salutation = Ms
    elseif Request("radiob utton") = "Mrs" then
    salutation = Mrs
    else
    salutation = Mr
    end if

    lastName=reques t("lastName")
    middleName=requ est("middleName ")
    firstName=reque st("firstName" )
    if Request("rbpost name")= "Jr" then
    postName=Jr
    else
    postName=Sr
    end if
    dateBorn=reques t("dateBorn")
    dateExpired=req uest("dateExpir ed")
    cemetaryDetails =request("cemet aryDetails")
    VisitationDetai ls=request("Vis itationDetails" )
    ServicesDetails =request("Servi cesDetails")

    if Request.Form("c urrent") = true then
    rstinsert("curr ent") = "Yes"
    else
    rstinsert("curr ent") = "No"
    end if
    ' dateExpired=Now ()
    sqlinsert = "insert into visitations(sal utation,lastNam e,middleName,fi rstName,postNam e,dateBorn,date Expired,cemetar yDetails,Visita tionDetails,Ser vicesDetails,cu rrent) values('" & request("radiob utton") & "','" & lastName & "' , '" & middleName & "', '" & firstName & "','" & request("rbpost name") & "','" & dateBorn & "','" & dateExpired & "','" & cemetaryDetails & "' ,'" & VisitationDetai ls & "','" & ServicesDetails & "', '" & current & "')"
    ' dbCon.Execute SQL_query
    rstinsert.Open sqlinsert,dbCon ,3,2
    ' response.Write( "added")
    Response.Redire ct("visitations .asp?valid=true ")
    'response.write (sqlinsert)
    end if
    %>
    [/code]
    may be i have done mistake in sqlinsert stmt pl correct if u have any idea.currect is the database yes/no field.
    Last edited by pbmods; Oct 25 '07, 01:04 PM. Reason: Added CODE tags
  • JamieHowarth0
    Recognized Expert Contributor
    • May 2007
    • 537

    #2
    Hi gudipati,

    This question should be in the ASP Forum, not the Articles section (Articles is only to be used for sharing information and not for asking questions).

    medicineworker

    Comment

    • jhardman
      Recognized Expert Specialist
      • Jan 2007
      • 3405

      #3
      Originally posted by gudipati
      [code=asp] if Request.Form("c urrent") = true then
      rstinsert("curr ent") = "Yes"
      else
      rstinsert("curr ent") = "No"
      end if[/code]
      These lines

      [code=asp]
      ' dateExpired=Now ()
      sqlinsert = "insert into visitations(sal utation,lastNam e,middleName,fi rstName,postNam e,dateBorn,date Expired,cemetar yDetails,Visita tionDetails,Ser vicesDetails,cu rrent) values('" & request("radiob utton") & "','" & lastName & "' , '" & middleName & "', '" & firstName & "','" & request("rbpost name") & "','" & dateBorn & "','" & dateExpired & "','" & cemetaryDetails & "' ,'" & VisitationDetai ls & "','" & ServicesDetails & "', '" & current & "')" [/code]
      don't match with these lines. It looks like maybe "rstinsert" is left over from a previous attempt or some other code snippet. This should work: [code=asp]dim current
      if Request.Form("c urrent") = true then
      current = "Yes"
      else
      current = "No"
      end if[/code][/quote]

      Comment

      • gudipati
        New Member
        • Oct 2007
        • 21

        #4
        hi i tried what u said.still i did't get.

        Comment

        • jhardman
          Recognized Expert Specialist
          • Jan 2007
          • 3405

          #5
          Originally posted by gudipati
          hi i tried what u said.still i did't get.
          which line gives you the error?

          Comment

          • markrawlingson
            Recognized Expert Contributor
            • Aug 2007
            • 346

            #6
            if Request.Form("c urrent") = true then
            Checkboxes do not return a boolean value. They return, by default, a value of "on" if they are checked, and do not return a value if they are not checked. If you set the value attribute of a checkbox, the checkbox will return that value if it is checked, but it still will not return a value if it is not checked.

            With that said, you're also setting your checkbox to a value of "" - so you can never tell whether it was checked or not. Since a checkbox that is not checked will not be returned in the Request.Form object - Request.Form("c urrent") will have a value of "" if it is not checked. If the checkbox IS checked, since you are setting its value="" it will also have a value of "" if it IS checked.

            Next...

            rstinsert("curr ent") = "Yes"
            If you are using access, which i'm assuming you are because you referred to it as a yes/no field, this value should be -1 for yes, and 0 for no.

            Here is the proper code.

            First your checkbox.
            [CODE=HTML]
            <input type="checkbox" name="current" id="current"/>
            [/CODE]

            And the ASP to get the result of the "current" checkbox.

            [CODE=ASP]
            If Request.Form("c urrent") = "on" then
            rstinsert("curr ent") = -1
            Else
            rstinsert("curr ent") = 0
            End If
            [/CODE]

            And finally a suggestion...

            In your access database, if you set the default value of a yes/no field to 0 - when a new record is inserted, the value of this field will be false/0/no etc, unless you specifically tell it that it is true/1/yes etc when you are inserting a new record. This make programming yes/no boxes much easier, and your coding much leaner.

            [Code=ASP]
            If Request.Form("c urrent") = "on" then
            rstinsert("curr ent") = -1
            End If
            [/CODE]

            In other words, if the checkbox is checked - set the yes/no field to yes. If it not checked, don't do anything because the default value of the yes/no field is automatically no, and we don't have to worry about telling it that it is supposed to be false if the checkbox is not checked.

            The last part, of course, is merely a suggestion.

            Comment

            Working...