<% 'Dimension variables Dim adoCon 'ADO Connection Object Dim strCon 'Holds the Database driver and the path and name of the database Dim strSQL 'Database query sring Dim strInputTitle 'Holds the URL Title Dim strInputURL 'Holds the URL Dim strInputDescription 'Holds the description of the URL Dim strInputKeywords 'Holds the keywords for the URL Dim blnNewURL 'Set to false if the URL has been entered before Dim saryDescriptionWord 'Array to hold each word in the description enetred by the user Dim intCheckWordLengthLoopCounter 'Loop counter Dim intWordLength 'Holds the length of the word to be checked Dim blnWordLenthOK 'Boolean set to False if any words in the description are above 30 characters Dim intLongestWordLength 'Holds the number of characters in the longest word entered in the description 'Error handler (stops the script crashing if there is an error) On error resume next 'Read in user deatils from the add URL form strInputTitle = Request.Form("title") strInputURL = Request.Form("url") strInputDescription = Request.Form("description") strInputKeywords = Request.Form("keywords") 'Where someones has pressed return in there description replace this with a space 'as otherwise the last word and first word on the line are seen as one word and may go over the 30 chrarcter limit strInputDescription = Replace(strInputDescription, vbCrLf, " ") 'Split-up each word in the description from the user to check that no word entered is over 30 characters saryDescriptionWord = Split(Trim(strInputDescription), " ") 'Initialse the word length variable blnWordLenthOK = True 'Loop round to check that each word in the description entered by the user is not above the 30 characters For intCheckWordLengthLoopCounter = 0 To UBound(saryDescriptionWord) 'Initialise the intWordLength variable with the length of the word to be searched intWordLength = Len(saryDescriptionWord(intCheckWordLengthLoopCounter)) 'Get the number of characters in the longest word If intWordLength => intLongestWordLength Then intLongestWordLength = intWordLength End If 'If the word length to be searched is more than or equal to 30 then set the blnWordLegthOK to false If intWordLength => 30 Then blnWordLenthOK = False End If Next 'Replace harmful characters in the form elements entered by the user that would cuase an SQL error when writting to the database strInputTitle = Replace(strInputTitle, "'", "''") strInputURL = Replace(strInputURL, "'", "''") strInputDescription = Replace(strInputDescription, "'", "''") strInputKeywords = Replace(strInputKeywords, "'", "''") 'Check URL doesn't already exsist in the database by calling the CheckNewURLDoseNotExsit function (at bottom of script) blnNewURL = CheckNewURLDoseNotExsit(strInputURL) 'If the URL exists then update the URL information in the database If blnNewURL = False Then 'If the URL exists then set the strSQL string to update the record in the database for that URL strSQL = "UPDATE tblWebsites SET " strSQL = strSQL & " Title='" & strInputTitle & "'" strSQL = strSQL & ", " & "Description='" & strInputDescription & "'" strSQL = strSQL & ", " & "Keywords='" & strInputKeywords & "'" strSQL = strSQL & ", " & "Date_Entered='" & Now() & "'" strSQL = strSQL & " WHERE URL ='" & strInputURL & "';" 'Else the URL is new so insert the new details in the Database Else 'Initialise the strSQL string with the SQL statment to insert the new URL in the database strSQL = "INSERT INTO tblWebsites ( Title, URL, Description, Keywords )" strSQL = strSQL & " VALUES" strSQL = strSQL & "('" & strInputTitle & "'" strSQL = strSQL & ", '" & strInputURL & "'" strSQL = strSQL & ", '" & strInputDescription & "'" strSQL = strSQL & ", '" & strInputKeywords & "');" End If 'If the user has entered no words over 30 characters then write to database If blnWordLenthOK = True Then 'Write to the database 'Create a connection odject to the database Set adoCon = Server.CreateObject("ADODB.Connection") 'Construct a connection string for the database Connection Object 'Use the Microsoft Access Driver for the Connection object strCon="DRIVER={Microsoft Access Driver (*.mdb)}; " 'Place in the Connection string the path and the name of the database, using the Server.MapPath method to get the path on the server to the database strCon = strCon & "DBQ=" & Server.MapPath("search_engine") 'Set an active connection to the Connection object adoCon.Open strCon 'Write to the database adoCon.Execute(strSQL) 'Close Sever Objects Set adoCon = Nothing End If 'Mensagem HTML %> Aicionar nova URL

Adicionar URL



<% 'If any of the words in the description are above 30 chracters then disoplay an error message If blnWordLenthOK = False Then Response.Write "Desculpe mas a URL " & strInputURL & " NÃO foi adicionada
Você usou em sua descrição, mais de " & intLongestWordLength & " caracteres, descrição muito longa!" Response.Write "

Editar a descrição enviada" 'If the URL has already been entered then display a message asying the URL has been updated ElseIf blnNewURL = False Then Response.Write "Obrigado por atualizar " & strInputURL & "
A URL foi atualizada" Response.Write "

Add another URL" 'Else display that the new URL has been entered into the database Else Response.Write "Obrigado por inserir" & strInputURL & "
a URL foi adicionada ao banco de dados" Response.Write "

Adicionar outra URL" End If %>

Retornar para a página de busca


<% 'Function to check that it is a new URL being entered into the database Private Function CheckNewURLDoseNotExsit(strInputURL) 'Dimension variables Dim adoCon 'ADO Connection Variable Dim adoRec 'ADO Recordset Variable Dim strAccessDB 'Holds the Access Database Name Dim strSQL 'Database query sring Dim strCheckURL 'Holds the name of the URL from the database to be checked 'Error handler On error resume next 'Intialise the CheckNewURLDoseNotExsit varible CheckNewURLDoseNotExsit = True 'Initialise the strAccessDB variable with the name of the Access Database strAccessDB = "search_engine" 'Create a connection odject to the database Set adoCon = Server.CreateObject("ADODB.Connection") Set adoRec = Server.CreateObject("ADODB.Recordset") 'Open connection to the database driver adoCon="DRIVER={Microsoft Access Driver (*.mdb)};" 'Open Connection to database adoCon = adoCon & "DBQ=" & server.mappath(strAccessDB) 'Initalise the strSQL variable with an SQL statement to get all the URLs from database strSQL = "SELECT URL FROM tblWebsites" 'Query the database adoRec.Open strSQL, adoCon ' loop through the records in the database to check the URL does't already exist ' using a Do While...Loop statement Do while not adoRec.EOF 'Read in the URLs form the database strCheckURL = adoRec("URL") 'Check a URL entered is not the same as the URL being checked in the database If strCheckURL = strInputURL Then 'If the URL is the same as one in the database then set the Function to False CheckNewURLDoseNotExsit = False End If ' Move to the next record adoRec.MoveNext Loop 'Close Sever Objects Set adoCon = Nothing Set adoRec = Nothing End Function %>