Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP
Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the fundamental ideas involved in using server-side code to write data to databases Objectives, by end of this week’s sessions, you should be able to: –create an ASP web page that allows the user to store data in database
Mark Dixon, SoCCE SOFT 131Page 3 Database Permissions 1 In order for ASP to write to a database –Need to give write access to Internet Guest Account for database file (People.mdb) Right-click on file in Windows Explorer (the following screens are for Windows 2000)
Mark Dixon, SoCCE SOFT 131Page 4 Database Permissions 2 Click Security tab Click Add button
Mark Dixon, SoCCE SOFT 131Page 5 Database Permissions 3 Select Internet Guest Account IUSR_ … Click Add button Click OK button
Mark Dixon, SoCCE SOFT 131Page 6 Database Permissions 4 Select Internet Guest Account Ensure write access is on
Mark Dixon, SoCCE SOFT 131Page 7 Writing data to a database create recordset open recordset –dynamic cursor, pessimistic locking to add a record –use to AddNew method rs.AddNew to delete a record –use the Delete method rs.Delete to change existing data –assign a new value to fields rs.Fields("Surname").Value = "Fred"
Mark Dixon, SoCCE SOFT 131Page 8 Example 1: Person Edit (html) Person's Details Person's Details <% ' ASP code will go here (next slide). %> Surname: " > PersonEdit.asp
Mark Dixon, SoCCE SOFT 131Page 9 Example 1: Person Edit (ASP) <% Const cs = "…" Dim rs Dim Surname Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs, 3, 3 If Session("curID") <> "" Then rs.Find "[ID] = " & Session("curID") If Request.Form("btnPrev") <> "" Then rs.MovePrevious ElseIf Request.Form("btnNext") <> "" Then rs.MoveNext ElseIf Request.Form("btnSave") <> "" Then rs. Fields("Surname") = Request.Form("txtSurname") rs. Update End If Session("curID") = rs.Fields("ID").Value Surname = rs.Fields("Surname").Value rs.Close Set rs = Nothing %>
Mark Dixon, SoCCE SOFT 131Page 10 People Database (with Hobbies) IDSurnameForenamesPhone 1DixonMark SmithJohn JonesSally BloggsFred AndersonGenny01752 HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person
Mark Dixon, SoCCE SOFT 131Page 11 SQL & MS access queries MS Access –Queries: select data from database –really SQL select statements –can use queries to test SQL code MS Access: People.mdb
Mark Dixon, SoCCE SOFT 131Page 12 SQL: Joining tables SELECT * FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; Two tables Matching records IDSurnameForenamesPhone HobbyIDDescriptionPersonID 1DixonMark DixonMark DixonMark DixonMark01752 people with swords1 2SmithJohn SmithJohn01752
Mark Dixon, SoCCE SOFT 131Page 13 SQL: DISTINCT records SELECT DISTINCT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; IDSurname 1Dixon 2Smith
Mark Dixon, SoCCE SOFT 131Page 14 Query Strings Data can be added to end of URL: ASP code can use this data: –Request.QueryString("Surname") would return the value "Bob" Form method=get –data automatically added to query string Query String