home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / form_to_db.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  6.6 KB  |  191 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. ' Just in case you're not up on my acronyms:
  15. ' DB = database, RS=recordset, CONN=connection
  16. ' o/w = otherwise
  17.  
  18. ' Include the VBScript ADO constants file
  19. %>
  20. <!-- #INCLUDE FILE="adovbs.inc" -->
  21. <%
  22. ' *** Begin DB Setup ***
  23. Dim strConnString
  24. ' Sample access OLEDB CONN String.
  25. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
  26.     Server.MapPath("db_scratch.mdb") & ";"
  27.         
  28. ' Override with our site-wide CONN string.
  29. strConnString = Application("SQLConnString")
  30.  
  31. ' Access likes #, but SQL Server expects ' so you'll need to change
  32. ' this based on the DB you're using.
  33. Const DATE_DELIMITER = "'"
  34. ' *** End DB Setup ***
  35.  
  36. Dim cnnFormToDB       ' CONN object
  37. Dim strSQL            ' String in which to build our SQL command
  38. Dim lngRecsAffected   ' # of records affected... just informational
  39.  
  40. ' Vars for the fields read in from the form.  All fields are read
  41. ' in as strings so I need to covert them to the appropriate data
  42. ' types to be sure they're appropriate for the DB fields.  These
  43. ' variables give me some working space to do this easily.
  44. Dim strTextField      ' text field
  45. Dim intIntegerField   ' integer field
  46. Dim datDateTimeField  ' date field
  47.  
  48. Dim strErrorMsg       ' Holds error message if we catch any problems.
  49.  
  50.  
  51. ' See if we have any info to process.
  52. ' If we don't (ie. the first time through) we just show
  53. ' the form.  If we do we proceed with the insert.
  54. If Request.Form("action") <> "Save Form Data" Then
  55.     ' Show the form
  56.     %>
  57.     <FORM ACTION="<%= Request.ServerVariables("SCRIPT_NAME") %>" METHOD="post">
  58.     <INPUT TYPE="hidden" NAME="action" VALUE="Save Form Data">
  59.     <TABLE BORDER="0">
  60.     <TR>
  61.         <TD ALIGN="right"><B>Text Field:</B></TD>
  62.         <TD ALIGN="left"><INPUT TYPE="text" NAME="text_field" MAXLENGTH="10"></TD>
  63.     </TR>
  64.     <TR>
  65.         <TD ALIGN="right"><B>Integer Field:</B></TD>
  66.         <TD ALIGN="left"><INPUT TYPE="text" NAME="integer_field"></TD>
  67.     </TR>
  68.     <TR>
  69.         <TD ALIGN="right"><B>Date/Time Field:</B></TD>
  70.         <TD ALIGN="left"><INPUT TYPE="text" NAME="date_time_field"></TD>
  71.     </TR>
  72.     <TR>
  73.         <TD> </TD>
  74.         <TD>
  75.             <INPUT TYPE="reset" VALUE="Clear">
  76.             <INPUT TYPE="submit" VALUE="Save">
  77.         </TD>
  78.     </TR>
  79.     </TABLE>
  80.     </FORM>
  81.     <%
  82. Else
  83.     ' Do our DB insert!
  84.  
  85.     ' Retrieve the 3 strings to be entered into the DB
  86.     strTextField     = Request.Form("text_field")
  87.     intIntegerField  = Request.Form("integer_field")
  88.     datDateTimeField = Request.Form("date_time_field")
  89.  
  90.     ' Start error handling... I'm too lazy to check all the criteria
  91.     ' on my own so I use VBScript to do it for me.  I simply do a
  92.     ' conversion the the expected type and if it fails I catch the
  93.     ' error, abort the insert, and display a message.
  94.     On Error Resume Next
  95.     strErrorMsg = ""
  96.     
  97.     ' String (text) field:
  98.     ' Nothing should really go wrong here.  It's already a string so
  99.     ' I don't bother with a CStr.  I do replace ' with '' for the
  100.     ' validity our SQL statement and also check to make sure it's
  101.     ' not an empty string.  If it is an empty string ("") then I
  102.     ' throw a fake error since I've already got this type of error
  103.     ' handling in place... hey I already admitted I was lazy!
  104.     strTextField = Trim(strTextField)
  105.     If Len(strTextField) = 0 Or Len(strTextField) > 10 Then Err.Raise 1
  106.     strTextField = Replace(strTextField, "'", "''")
  107.     If Err.number <> 0 Then
  108.         strErrorMsg = strErrorMsg & "Your entry for string_field is " & _
  109.             "inappropriate!<BR>" & vbCrLf
  110.         Err.Clear
  111.     End If
  112.  
  113.     ' Integer field:
  114.     ' Lots of possible problems here.  First off it could be text and
  115.     ' not a number at all!  Even if it is a number, there are a lot
  116.     ' of restrictions on integers.  It needs to be a whole number and
  117.     ' it's absolute value can't be bigger than around 32,000.  Using
  118.     ' CInt I don't have to worry about it though.
  119.     intIntegerField = CInt(intIntegerField)
  120.     If Err.number <> 0 Then
  121.         strErrorMsg = strErrorMsg & "Your entry for integer_field could " & _
  122.             "not be converted to an integer!  Remember that integers " & _
  123.             "need to be from -32,768 to 32,767.<BR>" & vbCrLf
  124.         Err.Clear
  125.     End If        
  126.     
  127.     ' Date field:
  128.     ' Well it needs to be a valid date.  You can enter it in any format
  129.     ' the computer can understand.  Doing the CDate will not only make
  130.     ' sure it's a date, but will also make them all nice and uniform!
  131.     datDateTimeField = CDate(datDateTimeField)
  132.     If Err.number <> 0 Then
  133.         strErrorMsg = strErrorMsg & "Your entry for date_time_field could " & _
  134.             "not be converted to an date variable!<BR>" & vbCrLf
  135.         Err.Clear
  136.     End If
  137.  
  138.     ' I don't know if this is really documented or a hack,
  139.     ' but it turns error trapping back off!
  140.     On Error Goto 0
  141.  
  142.     ' If we have an error in our error string then we show
  143.     ' the error message o/w we proceed with the insert.
  144.     If strErrorMsg <> "" Then
  145.         ' Show the error message that got us here!
  146.         Response.Write strErrorMsg
  147.     Else
  148.         ' Open connection to the DB
  149.         Set cnnFormToDB = Server.CreateObject("ADODB.Connection")
  150.         cnnFormToDB.Open strConnString
  151.  
  152.         ' Build our SQL String
  153.         strSQL = ""
  154.         strSQL = strSQL & "INSERT INTO scratch "
  155.         strSQL = strSQL & "(text_field, integer_field, date_time_field) " & vbCrLf
  156.         strSQL = strSQL & "VALUES ("
  157.         strSQL = strSQL & "'" & strTextField & "'"
  158.         strSQL = strSQL & ", "
  159.         strSQL = strSQL & intIntegerField
  160.         strSQL = strSQL & ", "
  161.         strSQL = strSQL & DATE_DELIMITER & datDateTimeField & DATE_DELIMITER
  162.         strSQL = strSQL & ");"
  163.  
  164.         ' Execute the SQL command.  I pass it a variable lngRecsAffected
  165.         ' in which to return the number of records affected.  I also tell
  166.         ' it that this is a text command and it won't be returing any
  167.         ' records... this helps it execute the script faster!
  168.         ' And before you ask... I don't know, but YES IT IS OR!!!
  169.         cnnFormToDB.Execute strSQL, lngRecsAffected, adCmdText Or adExecuteNoRecords
  170.  
  171.         ' Dispose of the CONN object
  172.         cnnFormToDB.Close
  173.         Set cnnFormToDB = Nothing
  174.         
  175.         ' Display a verification message and we're done!
  176.         %>
  177.         <H2>Thanks for submitting your information to us!</H2>
  178.         
  179.         <P>
  180.         <B>The resulting SQL statement was:</B>
  181.         <PRE><%= strSQL %></PRE>
  182.         </P>
  183.         
  184.         <P>
  185.         <B>Number of records affected:</B> <%= lngRecsAffected %>
  186.         </P>
  187.         <%
  188.     End If
  189. End If
  190. %>
  191.