<%
'declare the variables
Dim Recordset
Dim sql
dim Conn
Dim name1,email1,phone1,company1,title1
name1 = request.form("TxtName")
email1 = request.form("TxtEmail")
phone1 = request.form("TxtPhone")
company1 = request.form("TxtCompany")
title1 = request.form("TxtJob")
'create an instance of the ADO connection and recordset objects
Set Conn= Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")
'open the connection to the database
Conn.ConnectionString = "DSN=blah;User Id=...;Password=...;Database=...."
'Open the recordset object executing the SQL statement and return records
Recordset.Open
Conn.open
sql="INSERT INTO register (Name, email, phonenumber, company, title)"
sql=sql & "values ('"& name1 &"','"& email1 &"','"& phone1 &"','"& company1 &"','"& title1 &"')"
Conn.Execute(sql)
Conn.Close()
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample Registration Page</title>
</head>
<body>
<form name"form" action="">
<table>
<tr>
<td>Full Name:</td>
<td>
<input name="TxtName" id="TxtName" type="text" />
</td>
</tr>
<tr>
<td>Email:</td>
<td>
<input name="TxtEmail" id="TxtEmail" type="text" />
</td>
</tr>
<tr>
<td>Phone:</td>
<td>
<input name="TxtPhone" id="TxTPhone" type="text" />
</td>
</tr>
<tr>
<td>Company:</td>
<td>
<input name="TxtCompany" id="TxtCompany" type="text" />
</td>
</tr>
<tr>
<td>Job Title:</td>
<td>
<input name="TxtJob" id="TxtJob" type="text" />
</td>
</table>
<input name="button" ID="Button1" value="submit" type="Submit" />
</form>
</body>
</html>
我在运行此页面时收到error 500
消息,我不知道我的错误在哪里。
我也确实使用名为blah的名称建立DSN连接到我的SQL Server。
我单独运行ASP部分它可以工作,但数据库部分是我的问题所在。我非常感谢任何帮助,因为我对此比较陌生。
答案 0 :(得分:2)
首先,您应该在Web服务器上激活友好的错误显示,以便确切地知道错误的内容和位置,而不是通用的错误,比如说您现在得到的错误500。
其次,在此期间,添加一些Response.write,然后添加Response.Flush以查看其中的内容和位置;例如,显示sql字符串构建的结果:
sql = ...
response.write sql & "<br>"
response.flush
其次,您尝试打开没有关联的Command对象或sql查询字符串的Recordset;你不能这样做,事实上,你不需要任何Recordset,因为你有一个Insert查询,而不是Select查询。
最后,使用带有ADODB的DSN是一个坏主意。 DSN用于ODBC,并且通过在ADODB下使用ODBC,您将向数据连接添加旧的无用层。您应该为SQL-Server使用最新的本机OLEDB提供程序。在网络上搜索连接字符串,您将获得一些网站,其中包含可用提供商的完整详细信息及其连接字符串。
答案 1 :(得分:0)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample Registration Page</title>
</head>
<body>
<form action="registration.asp" method="POST" name="form1">
<table>
<tr>
<td>Full Name:</td>
<td><input name="TxtName" id="TxtName" type="text" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="TxtEmail" id="TxtEmail" type="text" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input name="TxtPhone" id="TxTPhone" type="text" /></td>
</tr>
<tr>
<td>Company:</td>
<td><input name="TxtCompany" id="TxtCompany" type="text" /></td>
</tr>
<tr><td>Job Title:</td>
<td><input name="TxtJob" id="TxtJob" type="text" /></td>
</table>
<input name="button" ID="Button1" value="submit" type="Submit" />
</form>
</body>
</html>
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" then
Dim Conn, Rs, ConnString, SQL_Insert
Dim name1,email1,phone1,company1,title1
name1 = request.form("TxtName")
email1 = request.form("TxtEmail")
phone1 = request.form("TxtPhone")
company1 = request.form("TxtCompany")
title1 = request.form("TxtJob")
Set Conn= Server.CreateObject("ADODB.Connection")
ConnString = "DSN=blah;User Id=...;Password=...;Database=...."
Conn.Open ConnString
SQL_Insert="INSERT INTO register (Name, email, phonenumber, company, title)" & _
"values ('"& name1 &"','"& email1 &"','"& phone1 &"','"& company1 &"','"& title1 &"');"
Conn.Execute SQL_Insert
Conn.Close
Set Conn = Nothing
Set SQL_Insert = Nothing
End If
%>
由于您没有从数据库中检索任何数据,因此不需要RecordSet。
将此代码复制并粘贴到文件&amp;将其命名为&#34; registration.asp&#34;
不要忘记用实际的
替换连接字符串for tutotrial访问www.w3schools.com
希望这有用
答案 2 :(得分:0)
SQL上似乎缺少空格字符。
sql = "INSERT INTO register (...)SPACE-MISSING-HERE"
sql = sql & "values (...)"
Conn.Execute(sql)
Conn.Close()