我有一個顯示包含用戶信息的用戶表的頁面。我希望能夠通過點擊每行末尾的按鈕來更新表中的一行。我對編碼完全陌生,所以如果有人能夠幫助我,並且「愚蠢」地回答他們的問題,我會非常感激。如何使用ADO更新SQL表中的行
謝謝!
這是我試過的代碼,但似乎無法使其工作。
<html>
<body>
<%
Dim conn, oRs, ID, sql
Set Conn=Server.CreateObject("ADODB.Connection")
ID = Request.Form("ID")
If Request.Form("ID")="" then
Set oRs=Server.CreateObject("ADODB.Recordset")
oRs.open "SELECT * FROM tICTStaff WHERE ID ='" & ID & "'",Conn
%>
<form method="post" action="employeeUpdate.asp">
<table>
<%for each x in oRs.Fields%>
<tr>
<td><%=x.name%></td>
<td><input name="<%=x.name%>" value="<%=x.value%>"></td>
<%next%>
</tr>
</table>
<br /><br />
</form>
<%
Else
sql="UPDATE table_name SET "
sql=sql & "Forename ='" & Request.Form("Forename") & "',"
sql=sql & "Surname ='" & Request.Form("Surname") & "',"
sql=sql & "Active ='" & Request.Form("Active") & "',"
sql=sql & "Address ='" & Request.Form("Address") & "',"
sql=sql & "DOB ='" & Request.Form("DOB") & "',"
sql=sql & " WHERE ID ='" & ID & "'"
on error resume next
Conn.Execute sql
If err<>0 then
Response.Write("Error updating Record!")
Else
Response.Write("Record " & ID & " was updated!")
End If
End if
Conn.close
%>
<meta HTTP-EQUIV="REFRESH" content="0; url=Page.index.asp?msg=The record has been updated!">
</body>
</html>
你提到了ADO,但用「asp-classic」標記了你的問題。你確定你是在經典的asp做這個嗎? – ulluoink
@ulluoink - 我不想讓你爲我寫代碼,那會很棒,但我不會通過這樣做來學習任何東西。我會將我嘗試過的代碼複製到原始文章中,但在提出最初問題時,我忘了這麼做。我正在用ASP編寫它,不確定它爲什麼會出現在ASP Classic中,我一定做錯了什麼。 – CodingNewbie
好吧你正在使用asp-classic。 – ulluoink