網上購物已成為生活的潮流,在網上購物之后,想要隨時查看自己已買的東西,想要隨時刪除或改動某件商品數量,要怎么做呢?以下我就來寫代碼及釋義。先來做用戶登陸頁面(login.asp):
<html>
<head>
<title>購物車的實現</title>
</head>
<body>
<formmethod="post"action="check.asp">
<tablewidth="200"border="1">
<tr><tdwidth="107">username:</td>
<tdwidth="77"><inputtype="text"name="username"id="username"/></td></tr>
<tr><td>password:</td>
<td><inputtype="password"name="userpass"id="userpass"/></td></tr>
<tr><tdcolspan="2"><inputtype="submit"value="登錄"></td>
</tr>
</table>
</form>
</body>
</html>
然后來寫接收用戶所填寫的信息以檢查是否存在該用戶的頁面(check.asp),假設存在該用戶,那么就跳到購物的首頁(index.asp):
<%
uname=request.Form("username")
upass=request.Form("userpass")
sql="select*fromuserswhereuname='"&uname&"'andupass='"&upass&"'"
setrs=server.CreateObject("adodb.recordset")
rs.opensql,conn,3,1
ifnotrs.eofthen
用戶登陸成功之后,將username放入session里,並給用戶分配一個訂單,就像去超市購物推着一個購物車,此時這個購物車是屬於你的,用username和系統時間作為購物車的名字,然后跳轉到購物首頁。
session("name")=uname
session("orderid")=time&uname
response.Redirect("index.asp")
else
%>
<script>
alert("username或password不對!");
history.back();
</script>
<%
endif
rs.close
setrs=nothing
conn.close
setconn=nothing
%>
那么,接下來就是購物的首頁(index.asp)了
<!--#includefile="inc/conn.asp"-->
<html>
<head>
以下用css樣式表將表格里的字體統一設置為14號字
<styletype="text/css">‘
<!--
table{
font-size:14px;
}
-->
</style>
</head>
<body>
<%
假設用戶沒登陸就直接打開購物首頁,那么就跳轉到登陸頁面
ifsession("name")=""then
response.Redirect("login.asp")
endif
%>
做一個搜索的文本框,方便用戶搜索商品。其原理是這種:用戶填入要搜索的商品后,通過表單提交到本頁面,從數據庫中查找像用戶填寫的字符串的商品,再顯示出來
<formmethod="post"action="index.asp">
<inputtype="text"size="15"name="search"id="search"/><inputtype="submit"value="搜索"/>
</form>
<%
searchname=request.Form("search")‘得到用戶填寫的字符串
sql="select*fromproductswherepnamelike'%"&searchname&"%'"‘寫sql語句,查找出結果
setrs=server.CreateObject("adodb.recordset")‘設置一個結果集對象
rs.opensql,conn,3,1
whilenotrs.eof‘循環輸出商品的相關信息
%>
每一個商品以下都有一個購買button,須要做一個表單
<formmethod="post"action="buy.asp">
<tablestyle="float:left">
<tr>
<tdwidth="103"rowspan="3"><ahref="product.asp?id=<%=rs(0)%>"><imgsrc="images/<%=rs(2)%>"width="100"height="100"border="0"></a><inputtype="hidden"name="id"value="<%=rs(0)%>"><inputtype="hidden"name="price"value="<%=rs(3)%>"/></td>
<tdwidth="107">名稱:
<%=rs(1)%></td>
</tr>
<tr>
<td>價格:<%=rs(3)%></td>
</tr>
<tr>
<tdalign="center"><inputtype="submit"name="buy"value="購買"/></td>
</tr>
</table>
</form>
<%
rs.movenext‘指向下一個記錄
wend
rs.close
setrs=nothing
conn.close
setconn=nothing
%>
</body>
</html>
假設用戶想要查看商品更具體的信息,那么就應該將商品圖片做成一個超鏈接,連接到product.asp,在該頁面顯示具體信息。在該頁面也有一個購買button,點擊之后相同跳到buy.asp:
<!--#includefile="inc/conn.asp"-->
<body>
<%
pid=request.QueryString("id")
response.Write(pid)
sql="select*fromproductswherepid="&pid
setrs=server.CreateObject("adodb.recordset")
rs.opensql,conn,3,1
ifnotrs.eofthen
%>
<tablewidth="387"height="119">
<tr>
<tdwidth="120"rowspan="4"><imgsrc="images/<%=rs(2)%>"width="100"height="100"></td>
<tdwidth="276">名稱:<%=rs(1)%></td>
</tr>
<tr>
<td>價格:<%=rs(3)%></td>
</tr>
<tr>
<tdheight="30"><%=rs(4)%></td>
</tr>
<tr>
<tdalign="center"><inputtype="button"onclick="javascript:location.href='buy.asp?id=<%=rs(0)%>&price=<%=rs(3)%>'"value="購買"></td>‘點擊購買之后觸發一個onclick事件,跳轉到buy.asp
</tr>
</table>
<%
endif
rs.close
setrs=nothing
conn.close
setconn=nothing
%>
</body>
</html>
用戶點擊了購買以后,將該商品的id傳到buy.asp,以下來寫buy.asp的代碼:
<!--#includefile="inc/conn.asp"-->‘將連接數據庫的字符串包括進來
<%
pid=request("id")‘得到商品的id
price=request("price")‘得到商品的價格
sql="select*frommrcarwherepid="&pid&"andorderid='"&session("orderid")&"'"‘寫sql語句來查詢
setrs=server.CreateObject("adodb.recordset")
rs.opensql,conn,3,1
假設用戶所要購買的商品已經買過,那么就直接在原來的基礎上加1,否則的話,就插入記錄
ifnotrs.eofthen
sql="updatemrcarsetpcount=pcount+1wherepid='"&pid&"'andorderid='"&session("orderid")&"'"
conn.executesql
response.Redirect("mycar.asp")
else
sql="insertintomrcar(orderid,pid,price,pcount)values('"&session("orderid")&"',"&pid&","&price&",1)"
conn.executesql
response.Redirect("mycar.asp")
endif
rs.close
setrs=nothing
conn.close
setconn=nothing
%>
運行完了sql語句之后將跳轉到購物車頁面(mycar.asp),顯示出用戶所購買的商品。而且有改動數量的button和刪除的超鏈接
<!--#includefile="inc/conn.asp"-->
<%
sql="selecta.*,b.pnamefrommrcara,productsbwhereorderid='"&session("orderid")&"'anda.pid=b.pid"
setrs=server.CreateObject("adodb.recordset")
rs.opensql,conn,3,1
%>
<formmethod="post"action="update.asp"name="form1">
<tableborder="1">
<tr><td>orderid</td>
<td>商品名稱</td>
<td>商品id</td>
<td>單位價格</td>
<td>數量</td>
<td>刪除</td>
<td>改動</td>
</tr>
<%
sum=0
i=1‘這里的i值到后邊有介紹
whilenotrs.eof‘將所購買的商品循環輸出
%>
<tr><td><%=session("orderid")%></td>
<td><%=rs("pname")%></td>
<td><%=rs(1)%></td>
<td><%=rs(2)%></td>
<td><inputtype="text"size="10"value="<%=rs(3)%>"name="pcount"></td>
<td><ahref="delete.asp?id=<%=rs(1)%>">刪除</a></td>
<td><inputtype="button"onclick="javascript:document.form1.action='update.asp?rowcount=<%=i%>&pid=<%=rs(1)%>';document.form1.submit();"value="確認改動"></td>
</tr>
<%
i=i+1
sum=sum+cint(rs(2))*rs(3)‘計算總價格
rs.movenext
wend
rs.close
setrs=nothing
conn.close
setconn=nothing
%>
</table>
</form>
<ahref="index.asp">返回繼續購物</a>
總金額為:<%=sum%>
點擊改動數量的button之后,就跳到update.asp:
<!--#includefile="inc/conn.asp"-->
<%
這里須要注意在上個頁面,有個i值,在這里就用到了,你要指明改動的是哪一行記錄
i=request.querystring("rowcount")
pcount=request.form("pcount")(i)
pid=request.querystring("pid")(i)
sql="updatemrcarsetpcount="&pcount&"wherepid="&pid&"andorderid='"&session("orderid")&"'"
conn.executesql
conn.close
setconn=nothing
response.redirect("mycar.asp")‘運行完之后又一次跳轉到mycar.asp
%>
相同點擊刪除之后,就跳到delete.asp,在該頁面得到商品的id
<!--#includefile="inc/conn.asp"-->
<%
pid=request.QueryString("id")
response.write(pid)
sql="deletefrommrcarwherepid="&pid&"andorderid='"&session("orderid")&"'"
conn.executesql
conn.close
setconn=nothing
response.Redirect("mycar.asp")‘運行完之后又一次跳轉到mycar.asp
%>一個簡單的購物車做好了,它用到了連接數據庫、數據庫的增、刪、改、查。
