|
Add to a SQL server database
In this example we are adding data to a simple
SQL server database called storename this consists of a table
called storename with two columns called name to store a user
name and email to store a users email address . We will simply
allow a user to add their name and email address to a form and
t hen submit the data which in turn gets stored in the database
.
Code :
Enter this and save it as storename1.php
<form method = "POST" action
= "storename2.php">
Name : <input type = "text" name = "name"><br>
Email : <input type = "text" name = "email"><br>
<input type = "submit"><input type = "reset">
</form>
Now enter the following and call it storename2.php
<%
'retrieve the values that the user entered first
Dim strName , strEmail
strName = Request.Form("name")
strEmail = Request.Form("email")
'now we will deal with the ado constants that are required
Const adOpenStatic = 3
Const adLockOptimistic = 3
'now our object variables and we will create instances of the
objects
Dim objConn , objRS , strSQL
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")
'connect to our database called storename , we are using the
sa User ID in this
'example , you will also have to insert your server name in
the Data Source
objConn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;"&
_
"User ID=sa;Initial Catalog=storename;Data Source=YOURDATASOURCE"
'select all entries from the storename table
strSQL = "SELECT * FROM storename"
'execute the SQL query
objRS.Open strSQL , objConn , adOpenStatic , adLockOptimistic
' add a new record
objRS.AddNew
'store the strName value in the name column
objRS("name") = strName
'store the strEmail value in the email column
objRS("email") = strEmail
'update the database
objRS.Update
'close our objects
objRS.Close
objConn.Close
'destroy our objects
Set objRS = Nothing
Set objConn = nothing
%>
We cannot show an example as we do not have SQL
server support yet but remember and change the Data Source to
whatever your server name is and also in this example we are
using the sa account on SQL server , for security reasons this
is not recommended but since this is only for testing purposes
we have used it , normally you would use secure login information.
|