Just nu har jag et script i ASP där jag tidigare använde mig utav en .mdb-databas.
Nu har jag gjort en .sql databas som jag iställt ska använda.
Hur ska jag anropa .sql-databasen?
Så här ser sidorna ut:
Rating.asp
Kod:
<html>
<head>
<title>Top Cat</title>
<link rel="stylesheet" href="stil.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body,td,th {
font-family: Tahoma;
font-size: 12px;
color: #FFFFFF;
}
body {
background-color: #000000;
}
a:link {
color: #C0C0C0;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #C0C0C0;
}
a:hover {
text-decoration: none;
color: #4F4F4F;
}
a:active {
text-decoration: none;
color: #4F4F4F;
}
-->
</style></head>
<body>
<div align="center">
<%'###Antal inlägg per sida######
ANTAL = "20"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" &Server.MapPath("db/rating.mdb")
Set RecSet = Server.CreateObject("ADODB.RecordSet")
Sidan = Request.QueryString("next")
If Sidan = "" then
sida = 1
Else
sida = Sidan
End If
With RecSet
.CursorLocation = 3
.CacheSize = ANTAL
End With
SQL = "SELECT * FROM rating ORDER by id desc"
RecSet.Open SQL, Conn%>
</div>
<table width="450" border="0" bordercolor="#FFC000" cellspacing="2" align="center">
<tr height="20">
<td width="90" valign="top" bgcolor="#333333"><b>Betyg:</b></td>
<td width="100" valign="top" bgcolor="#333333"><b>Bild:</b></td>
<td width="150" bgcolor="#333333"><b>Deltagar info:</b></td>
<td width="35" align="center" bgcolor="#333333" betygign="top"><b>Röster:</b></td>
<td width="35" align="center" bgcolor="#333333" betygign="top"><b>Snitt: </b></td>
<td width="40" align="right" valign="top" bgcolor="#333333"><b>RÖSTA</b></td>
</tr>
<%If RecSet.EOF Then%>
<tr>
<td colspan="6">Inget i databasen</td>
</tr>
<%Else
With RecSet
.MoveFirst
.PageSize = ANTAL
.AbsolutePage = sida
End With
Sidor = RecSet.PageCount
x = 0
Do While Not RecSet.EOF And x < RecSet.PageSize
If RecSet("snitt") = 0 Then
Bild = "<img src=bilder/star.gif>"
ElseIf RecSet("snitt") >= 1 And RecSet("snitt") < 2 Then
Bild = "<img src=bilder/star1.gif>"
ElseIf RecSet("snitt") >= 2 And RecSet("snitt") < 3 Then
Bild = "<img src=bilder/star2.gif>"
ElseIf RecSet("snitt") >= 3 And RecSet("snitt") < 4 Then
Bild = "<img src=bilder/star3.gif>"
ElseIf RecSet("snitt") >= 4 And RecSet("snitt") < 5 Then
Bild = "<img src=bilder/star4.gif>"
ElseIf RecSet("snitt") = 5 Then
Bild = "<img src=bilder/star5.gif>"
End If%>
<tr height="15">
<td valign="top" width="90"><%=bild%></td><td valign="top" width="100"><b><a href="<%=RecSet("link")%>" target="_blank"><img src="" alt="" name="bild" width="90" height="90" border="0" style="background-color: #000000"><%=RecSet("link")%></a></b></td>
<td width="150"><%=RecSet("info")%></td><td valign="top" align="center" width="35"><%=RecSet("total")%></td><td valign="top" align="center" width="35"><%=Round(RecSet("snitt"),2)%></t d><td valign="top" align="right" width="40"><b><a href="rating.asp?id=<%=RecSet("id")%>"><img src="rosta.gif" width="39" height="11" border="0"></a></b></td>
</tr>
<%If CStr(Request.QueryString("id")) = CStr(RecSet("id")) Then%>
<tr>
<td colspan="6">
<table width="262" align="center" cellspacing="0"><tr>
<td valign="top" ><b>Välj betyg: </b></td><td valign="top"><b><a href="rosta.asp?id=<%=RecSet("id")%>&betyg=1" ><img src="1.gif" width="12" height="11" border="0"></a></b></td>
<td valign="top"><b><a href="rosta.asp?id=<%=RecSet("id")%>&betyg=2" ><img src="2.gif" width="12" height="11" border="0"></a></b></td>
<td valign="top"><b><a href="rosta.asp?id=<%=RecSet("id")%>&betyg=3" ><img src="3.gif" width="12" height="11" border="0"></a></b></td>
<td valign="top"><b><a href="rosta.asp?id=<%=RecSet("id")%>&betyg=4" ><img src="4.gif" width="12" height="11" border="0"></a></b></td>
<td valign="top"><b><a href="rosta.asp?id=<%=RecSet("id")%>&betyg=5" ><img src="5.gif" width="12" height="11" border="0"></a></b></td>
</tr></table>
</td>
</tr>
<%End If
x=x+1
RecSet.MoveNext
Loop
If Sidor > 1 Then%>
<tr bgcolor="#333333">
<td colspan="6" valign="top">
<%If (Int(sida)) <> 1 Then%>
<a href="rating.asp?next=<%=(Int(sida)-1)%>"><b>Bakåt</b></a> |
<%Else%><%End If%>
<%For Sid = 1 To Sidor
If (Int(Sid)) = (Int(sida)) Then%><font color="#000000"><%=Sid%></font><%Else%> <a href="rating.asp?next=<%=Sid%>"><%=Sid%></a> <%End If
Next%>
<%If (Int(sida)) <> (Int(Sidor)) Then%>
| <a href="rating.asp?next=<%=(Int(sida)+1)%>"><b>Framåt</b></a>
<%Else%><%End If%>
</td>
</tr>
<%End If
End If
Set RecSet = Conn.Execute("SELECT Sum(total) As Tot FROM rating")%>
</table>
<div align="center">
<%RecSet.Close
Conn.Close
Set RecSet = Nothing
Set Conn = Nothing%>
</div>
</body>
</html>
Rosta.asp
Kod:
<%Set Conn = Server.CreateObject("ADODB.Connection")
Set RecSet = Server.CreateObject("ADODB.Recordset")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db/rating.mdb")
If Request.Cookies(Request.Querystring("id")) = "true" Then
Response.Redirect "rating.asp"
Else
SQL = "SELECT * From rating Where id=" & Request.Querystring("id")
RecSet.Open SQL, Conn
betyg = Request.QueryString("betyg")
SQL = "Update rating Set Summa = Summa+" &betyg &" Where id =" & Request.Querystring("id")
Conn.Execute(SQL)
SQL = "Update rating Set r"&betyg&"=r"&betyg&"+1 Where id=" & Request.Querystring("id")
Conn.Execute(SQL)
SQL = "Update rating Set Total = Total + 1 Where id = " & Request.Querystring("id")
Conn.Execute(SQL)
SQL = "Update rating Set Snitt ="& RecSet("Summa")&"/"&RecSet("Total") &" Where id=" & Request.Querystring("id")
Conn.Execute(SQL)
RecSet.Close
Conn.Close
Response.Cookies(Request.Querystring("id")) = "true"
Response.Cookies(Request.Querystring("id")).Expires = DateAdd("h", 1, now)
Response.Redirect "rating.asp"
End If%>
Admin.asp
Kod:
<%Response.Buffer = True
Admin = "Admin"
Losen = "Losen"%>
<html>
<head>
<title>Logga in</title>
<link rel="stylesheet" href="stil.css" type="text/css">
</head>
<%If Request.QueryString("visa") = "" Then%>
<table width="200" align="center" border="0">
<tr>
<td colspan="2"><font size="2"><b>Logga in:</b></font></td>
</tr>
<tr>
<td height="10" colspan="2"><hr></td>
</tr>
<form name="admin" method="post" action="admin.asp?visa=loginkoll">
<tr>
<td><b>Adminnamn:</b></td><td><input type="text" name="admin1" size="20"></td>
</tr>
<tr>
<td><b>Adminpass:</b></td><td><input type="password" name="admin2" size="20"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login"> <a href="rating.asp"><b>Tillbaka</b></a></td>
</tr>
<%If Session("mess") <> "" Then%>
<tr>
<td height="10" colspan="2"><hr></td>
</tr>
<tr>
<td colspan="2" align="center">
<%Response.Write Session("mess")
Session("mess") = ""%>
</td>
</tr>
<%End If%>
</form>
</table>
<%End If
If Request.QueryString("visa") = "loginkoll" Then
If Request.Form("admin1") = Admin AND Request.Form("admin2") = Losen Then
login="ok"
Else
Session("mess") = "Fel adminnamn eller lösenord."
Response.Redirect "admin.asp"
End If
If login="ok" Then
Session("admin") = "ok"
Session.TimeOut = 40
Response.Redirect "admin.asp?visa=inne"
End If
End If
If Request.QueryString("visa") = "inne" Then%>
<table width="200" align="center" border="0">
<tr>
<td colspan="2"><font size="2"><b>Inloggad:</b></font></td>
</tr>
<tr>
<td height="10" colspan="2"><hr></td>
</tr>
<%Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" &Server.MapPath("db/rating.mdb")
Set RecSet = Conn.Execute("SELECT * FROM rating ORDER by id desc")
If RecSet.EOF Then%>
<tr>
<td height="10" colspan="2">Inga </td>
</tr>
<%End If
Do Until RecSet.EOF%>
<tr>
<td><b><a href="<%=RecSet("url")%>" target="_blank"><%=RecSet("link")%></a></b></td><td align="right"><a href="admin.asp?visa=tabort&id=<%=RecSet("id")%>">Ta bort</a></td>
</tr>
<%RecSet.MoveNext
Loop%>
<tr>
<td height="10" colspan="2"><hr></td>
</tr>
<tr>
<td></td><td align="right"><a href="admin.asp?visa=logut"><b>Logga ut</b></a> <a href="admin.asp?visa=laggtill"><b>Lägg till</b></a></td>
</tr>
</table>
<%End If
If Request.QueryString("visa") = "laggtill" Then%>
<table width="200" align="center" border="0">
<form method="post" action="admin.asp?visa=spara">
<tr>
<td><font size="2"><b>Lägg till:</b></font></td>
</tr>
<tr>
<td height="10"><hr></td>
</tr>
<tr>
<td><b>Namn:</b><br>
<input type name="link" size="35"></td>
</tr>
<tr>
<td><b>URL:</b><br>
<input type name="url" size="35"></td>
</tr>
<tr>
<td><b>Info:</b><br><textarea name="info" rows="5" cols="35"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Lägg till"></td>
</tr>
<tr>
<td height="10"><hr></td>
</tr>
<tr>
<td align="right"><a href="admin.asp?visa=logut"><b>Logga ut</b></a> <a href="admin.asp"><b>Tillbaka</b></a></td>
</tr>
</form>
</table>
<%End If
If Request.Querystring("visa") = "spara" Then
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" &Server.MapPath("db/rating.mdb")
Url = Request.Form("url")
If Url <> "" Then
If Left(Url,7) = "http://" Then
Url = Url
Else
Url = "http://" & Url
End If
End If
SQL = "Insert Into rating (link,url,info) Values('" & Request.Form("link") & "','" & Request.Form("url") & "','" & Request.Form("info") & "')"
Conn.Execute(SQL)
Response.Redirect "admin.asp?visa=inne"
End If
If Request.Querystring("visa") = "tabort" Then
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" &Server.MapPath("db/rating.mdb")
SQL = "Delete From rating Where id ="& Request.Querystring("id")
Conn.Execute(SQL)
Response.Redirect "admin.asp?visa=inne"
End If
If Request.Querystring("visa") = "logut" Then
Session.Abandon
Response.Redirect "rating.asp"
End If%>
</body>
</html>