![]() |
|
|
Creating a News management system (Part 5)We now want to add news. The way we will do that is by creating a web form which will allow us to enter news details. These details will posted to a second page 'insert_news.asp' and inserted into the database. Have a look at the code below. 'Add_news.asp' <% 'Our login protection code If Session("BlnLoggedIn") <> True Then Response.Redirect("login.asp") End If %> <html> <head> <title>News Management System - Add News</title> </head> <body> <!--#include file="admin_navigation.asp" --> <div align='center'> <form name="form" method="post" action="insert_news.asp"> <br> <table width="334"> <tr> <td width="199"> headline: <br> </td> <td width="123"> <input type="text" name="headline" maxlength="50"> </td> </tr> <tr> <td> news story: </td> <td> <textarea name="news_story" cols="35" rows="7"></textarea> </td> </tr> <tr> <td height="32" colspan="2"> <div align="center"> <input type="submit" name="Submit" value="Submit"> <input type="reset" name="reset" value="reset"> </div> </td> </tr> </table> </form> </div> </body> </html>
Save 'add_news.asp' in the 'admin' folder. Next we need a page that will process the news submitted. This will be the action page called 'insert_news.asp'. Here is what the code looks like for our page 'insert_news.asp'. <%@ Language="VBScript" %>
<% Option Explicit %> <% 'Our login protection code If Session("BlnLoggedIn") <> True Then Response.Redirect("login.asp") End If %> <html> <head><title>News Management System - News Added</title> </head> <body> <!--#include file="admin_navigation.asp" --> <% 'declare your variables Dim Connection, sConnString Dim headline, news, thedate, SQL 'receive values sent by form headline=request.form("headline") news=request.form("news_story") ' the date will be generated by the Date function rather than sent by the form thedate=Date() 'declare SQL statement that will query the database SQL = "INSERT INTO tblNews(headline,News_Story,Story_date) Values " & _ "( '" & headline & "', '" & news & "', '" & thedate & "')" 'Create ADO connection and recordset object Set Connection = Server.CreateObject("ADODB.Connection") 'define the connection string, specify database 'driver and the location of database sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("../db/news.mdb") 'Open the connection to the database Connection.Open(sConnString) 'execute the SQL statement Connection.execute(SQL) 'lets close the connection object connection.Close Set connection = Nothing 'Let us know the news story has been submitted response.write "<br><div align='center'>Your news has been entered</div>" %> </body> </html> Again save 'insert_news.asp' in the 'admin' folder. Notice in the code above we don't use the recordset object as we are not returning any records. Our next page will allow us to delete news.
Membership Software Integrates with PayPal
Site developed by Michael Wall - Web Design Belfast N.Ireland. |
|