Add to a database

In this example we will add a url to our sample database . Note there is no validation yet in this example (future version) but this shows how easy it is to add to a database , you could use this as part of a primitive link page system.

Anyway the first part is our form

 

Here is the code for this

<FORM method = POST ACTION ="addtodb.php">
URL<input type = "text" name = "url"><br>
Description<input type = "text" name = "desc"><br>
<input type = "submit" value="submit">
<input type = "reset" value = "reset">
</form>

Nothing difficult here but note the addtodb.php , this is the script that does all the work . Lets have a look at it .

<%
'declare our variables
Dim objConn , objRS
'create instances of the objects
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")
'our connection string
objConn.Open "DBQ=" & Server.MapPath("sampledb.mdb")& ";Driver={Microsoft Access Driver (*.mdb)}"
strSQL = "SELECT * FROM tblsample"
objRs.Open strSQL , objConn , 3 , 3
'declare 2 variables to store the data from our form
Dim strUrl , strDesc
strUrl = Request.Form("url")
strDesc = Request.Form("desc")
objRS.AddNew
'send form data to our database
objRS("url") = strUrl
objRS("desc") = strDesc
'update the recordset
objRs.Update
'close recordset and delete object variables
objRs.Close
Set objConn = Nothing
Set objRS = Nothing
'redirect back to form page
Response.Redirect "addtodatabase.php"
%>

 

Sponsors