Jump to content

Not even sure where to start


Belzar

Recommended Posts

Here is my problem. I want to make a forum page, where the people can say whatever they want and however they want to say it. I guess you can say a "blog" page for people that come to my site. I am not sure what code to use. I don't even know where to start for this. I would really appreiciate it if you could help me. Thanks.

Link to comment
Share on other sites

Here is my problem. I want to make a forum page, where the people can say whatever they want and however they want to say it. I guess you can say a "blog" page for people that come to my site. I am not sure what code to use. I don't even know where to start for this. I would really appreiciate it if you could help me. Thanks.

Sounds almost like you're writing a guestbook, which is a pretty good first project if you're new to ASP.It is a prerequisite to know HTML before getting started. Its also a prerequisite that you make sure you have a place to host your website. You can't put your site on Geocities, Tripod, or just any website, you have to find a host that has ASP installed. Generally, the best host for that solution is Brinkster, because they're the most reliable and have the fastest servers.Next, you want to do is create a database. If you're planning to use Brinkster, then you can't use a database more powerful than MS Access. Its fine to use Access if you plan to have less than 10 people viewing your site at any given time. If you wanted to create a website like My Miserable Life, a website that is based on the same concept as yours above, then you need a more robust database like MySQL or SQL Server. You don't get those kinds of databases for free, usually you have to buy webspace, which will cost about US$5 a month.(I'll presume you're using Access from hear on out.)Now, before you can get anything started with your site, you have to design a table. For the kind of website you're proposing, you don't need to make anything complicated. Open MS Access, start with a blank database, save it as "mydb.mdb". When get to the main screen, select the option which says "Create a new table in DESIGN view", and enter the following fields:
Field          Type              OptionsID             Autonumber        Primary KeyAuthor         Text              Maxlength=20Content        MemoPosted         Date/Time

Save that table as Guestbook. Its important to be sure that none of your table fields, or you table names have spaces in them.After you've created your table, type a few names, dates, and a few words into each field (make sure when you type the dates, you use a valid date format such as YYYY-MM-DD). That data will be used as test data when we build the page; you can always delete it when your done.At this point, you can start building a website. Here's a sample page, name it default.asp and upload it to your webhost (its important that whatever you name your website has the .asp extension):

<% Option Explicit %><html><head><title>My site</title></head><body><h1>This is my site</h1><p>Write something!</p><!-- We are going to display the contents of our database here. --></body></html>

So far, there is nothing that appears on the page, except for the a mysterious "<% Option Explicit %>". The "<%" and "%>" are the ASP delimeters, anything that appears between those two tags will be interpreted by the server and executed. All of your programming code goes between the ASP delimters. For now, the only code that exists is the "Option Explicit" statement, which just tells the server to force variable declaration. If you are trying to use a variable that hasn't been declared, ASP will display an error reading "Undeclared variable at line ###".I'm not sure how much programming experience you have, so I'm going to divert away from the Guestbook program for just a moment and explain the importance of declaring your variables:Usually, people think its a pain to be forced to declare variables, but its actually very important, and it saves time in development. For instance, if you misspell the name of a variable without using Option Explicit, then ASP will treat it as just a new variable; so when you use that unknowingly misspelled variable, you get incorrect results, such as an empty string when you were expecting a name or a word. When this happens, you have to hunt down the misspelled variable, change it, and run your program again; if you have an exceptionally long program, such as something that is 400 or 500 lines long, and perhaps spans over several pages, you could very easily misspell your variable a number of times and be forced to hunt down each instance of a misspelling. And that's no fun at all. Using Option Explicit will do all of that work for you, so get faster development.Now back to your guestbook:We're ready to display some of the data from your database now. This always requires us to create four variables, two them are going to be special variables called Objects.

<% Option Explicit %><html><head><title>My site</title></head><body><h1>This is my site</h1><p>Write something!</p><%' Use the Dim statement to declare variables'' You can declare several variables with successive Dim statements like this:'   Dim var1'   Dim var2'   Dim var3'   ...'' Or you can declare several variables in one line, seperating each variable with a comma:'   Dim var1, var2, var3''<--- Oh, and these apostrophes create comments in your code. They are a way of'helping you remember what logic you used to create a program, but they the ASP'interpreter will just overlook them.'' Also, when you create a page, the ASP code you've used to write it will be invisible. It' will be converted into HTML.'Dim DSNName    'This is variable that will hold the information to connect to the databaseDim Conn    'This is the "connection" variable, it will use the DSNName variable to            'connect to the database.Dim RS      'This is the recordset variable, it will be used to access the records of the            'database which was created earlier.Dim sql     'This is a variable that will hold SQL statements.'These next three lines set the path to your database. Of course, you'll want to set'the fields to location of you own database. If you are using Brinkster, then you are'required to put the database in the db/ folder (its the only one with read/write'permissions).DSNName = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="DSNName = DSNName & Server.MapPath("db/mydb.mdb")      'The "&" symbol is a way ofDSNName = DSNName & ";PWD=mypass"                      'joining, or contatenating                                               'two strings together.'Now we are going to create our objects:Set Conn = Server.CreateObject("ADODB.Connection")Set RS = Server.CreateObject("ADODB.Recordset")Conn.Open DSNName        'Hopefully, even if you don't know SQL, the following statement will be obvious    'to you. All of the fields after the SELECT keyword correspond to the fields you    'Created in your database, the FROM keyword specifies which table that fields    'correspond to. The ID DESC statement means you are going to put your records    'in descending order; because all of your IDs will numbers (such as 1,2,3,4,5),    'putting them descending order will rearrange the records by ID so that    'the record with the highest ID is at the top and the record with lowest ID is    'at the bottom (such as 5,4,3,2,1). This will correspond to ordering your     'records from Newest to Oldest.    sql = "SELECT ID, Author, Content, Posted FROM Guestbook ORDER BY ID DESC"        RS.Open sql, Conn, 1, 1       'The RS.Open statement puts all of the records from your database into an       'array. It executes the SQL statement above. The "1, 1," at the end of the       'statement tell the database that we'll be opening it in read-only mode,        'without making any further changes to the database.       'Now we just have to loop through all the records we've created. We're going       'to put these records into a two-column table. I'm putting the HTML tags       'outside of the ASP delimeters for the sake of simplicity.       %>       <table width="100%">       <%       Do until rs.eof        'This tells the database to loop through all the                              'records in recordset.           %>           <tr>                <td align="left" valign="top">                    <p>                        <%=RS("Author")%><br>                        <%=RS("Posted")%>                    </p>                </td>                <td align="left" valign="top">                    <%=Replace(RS("Content"), vbNewline, "<br>", 1, -1, vbBinaryCompare)%>                </td>           </tr>           <%           'Those mini-%= statements above are actually equivalent to the ASP           'Response.write statement, which outputs text to the window. In this           'case, they are just writing the fields from the database. The Replace           'statement above transforms all the linebreaks the Content field into           '<br>s, so that they can be interpreted by the browser.           RS.Movenext        'This tells the database to go to the next record.                              'If you omit the RS.Movenext statement, you get an                              'infinite loop. Trust me, you don't like infinite                              'loops.       Loop       %>       </table>       <%    RS.CloseConn.Close'ALWAYS ALWAYS ALWAYS clear the objects you've created from memory by setting them'equal to nothing when you've finished with them.set conn = nothingset rs = nothing%></body></html>

That's how to display records in a database. Its important realize that *all* of the records from your database will be displayed one page; recordpaging takes a little more work, which would be overwhelming for a newbie.The code above looks intimidating, but in actuality its only 20 lines actual code with my comments taking up the bulk of it; the ASP is actually spaced out quite a bit for simplicity, the same program can be written in about 15 lines.I should also mention a disclaimer: no web developers use the RS(fieldname) method, called recordset iteration, to get contents out of a database, because while it is very simple, its also extremely inefficient (see AspFaq #2467 for an explanation); when I call information from a database, I almost never use Recordset iteration, I use the GetRows() feature to cram all of my information into a 2-dimensional array, then display it. I would have shown you the GetRows() method, but multidimensional arrays are just too much work for your first ASP project.Now, I'm going to show you how to insert records into your database. You need to have an HTML form somewhere on your page, preferably above all of your records. Just use the following HTML:

<%Option Explicit %><html><head><title>My site</title></head><body><h1>This is my site</h1><p>Write something!</p><form action="post.asp" method="post"><p>Name:<br><input type="Text" name="author"></p><p>Comment:<br><textarea rows="10" cols="50" name="Content"></textarea></p></form><hr>...

Everything else on the page will stay the same.Now, you are going to create a page called Post.asp, which will insert the contents of form on the previous page into your Guestbook table. The code for this page is actually very simple:

<% Option ExplicitDim DSNName, Conn, RS, sqlDim strAuthor, strContentstrAuthor = request("Author")     'The request keyword is used to access the                             'querystringstrContent = request("Content")   'data that you send from page to page.DSNName = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="DSNName = DSNName & Server.MapPath("db/mydb.mdb")DSNName = DSNName & ";PWD=mypass"Set Conn = Server.CreateObject("ADODB.Connection")Set RS = Server.CreateObject("ADODB.Recordset")Conn.Open DSNName    sql = "SELECT ID, Author, Content, Posted FROM Guestbook Where 0=1"    'The SQL statement above just opens the table without returning any records.    'We don't need it to return any records, because all we're doing is adding a    'new record.     RS.Open sql, Conn, 3, 3          'The "3, 3" tells the database that we'll be adding a new record to           'database.                    RS.AddNew          RS("Author") = strAuthor          RS("Content") = strContent          RS("Posted") = Now    'The now keyword inserts the date and time          RS.Update     RS.CloseConn.Closeset conn = nothingset rs = nothing'That's all there is to adding new records to the database. Now you have to 'return back to your previous page. You use the Response.redirect statement'to do that:Response.Redirect "default.asp"%>

Now you're done. You have a guestbook for your site.

Link to comment
Share on other sites

Sounds almost like you're writing a guestbook, which is a pretty good first project if you're new to ASP.It is a prerequisite to know HTML before getting started. Its also a prerequisite that you make sure you have a place to host your website. You can't put your site on Geocities, Tripod, or just any website, you have to find a host that has ASP installed. Generally, the best host for that solution is Brinkster, because they're the most reliable and have the fastest servers.Next, you want to do is create a database. If you're planning to use Brinkster, then you can't use a database more powerful than MS Access. Its fine to use Access if you plan to have less than 10 people viewing your site at any given time. If you wanted to create a website like My Miserable Life, a website that is based on the same concept as yours above, then you need a more robust database like MySQL or SQL Server. You don't get those kinds of databases for free, usually you have to buy webspace, which will cost about US$5 a month.(I'll presume you're using Access from hear on out.)Now, before you can get anything started with your site, you have to design a table. For the kind of website you're proposing, you don't need to make anything complicated. Open MS Access, start with a blank database, save it as "mydb.mdb". When get to the main screen, select the option which says "Create a new table in DESIGN view", and enter the following fields:
Field          Type              OptionsID             Autonumber        Primary KeyAuthor         Text              Maxlength=20Content        MemoPosted         Date/Time

Save that table as Guestbook. Its important to be sure that none of your table fields, or you table names have spaces in them.After you've created your table, type a few names, dates, and a few words into each field (make sure when you type the dates, you use a valid date format such as YYYY-MM-DD). That data will be used as test data when we build the page; you can always delete it when your done.At this point, you can start building a website. Here's a sample page, name it default.asp and upload it to your webhost (its important that whatever you name your website has the .asp extension):

<% Option Explicit %><html><head><title>My site</title></head><body><h1>This is my site</h1><p>Write something!</p><!-- We are going to display the contents of our database here. --></body></html>

So far, there is nothing that appears on the page, except for the a mysterious "<% Option Explicit %>". The "<%" and "%>" are the ASP delimeters, anything that appears between those two tags will be interpreted by the server and executed. All of your programming code goes between the ASP delimters. For now, the only code that exists is the "Option Explicit" statement, which just tells the server to force variable declaration. If you are trying to use a variable that hasn't been declared, ASP will display an error reading "Undeclared variable at line ###".I'm not sure how much programming experience you have, so I'm going to divert away from the Guestbook program for just a moment and explain the importance of declaring your variables:Usually, people think its a pain to be forced to declare variables, but its actually very important, and it saves time in development. For instance, if you misspell the name of a variable without using Option Explicit, then ASP will treat it as just a new variable; so when you use that unknowingly misspelled variable, you get incorrect results, such as an empty string when you were expecting a name or a word. When this happens, you have to hunt down the misspelled variable, change it, and run your program again; if you have an exceptionally long program, such as something that is 400 or 500 lines long, and perhaps spans over several pages, you could very easily misspell your variable a number of times and be forced to hunt down each instance of a misspelling. And that's no fun at all. Using Option Explicit will do all of that work for you, so get faster development.Now back to your guestbook:We're ready to display some of the data from your database now. This always requires us to create four variables, two them are going to be special variables called Objects.

<% Option Explicit %><html><head><title>My site</title></head><body><h1>This is my site</h1><p>Write something!</p><%' Use the Dim statement to declare variables'' You can declare several variables with successive Dim statements like this:'   Dim var1'   Dim var2'   Dim var3'   ...'' Or you can declare several variables in one line, seperating each variable with a comma:'   Dim var1, var2, var3''<--- Oh, and these apostrophes create comments in your code. They are a way of'helping you remember what logic you used to create a program, but they the ASP'interpreter will just overlook them.'' Also, when you create a page, the ASP code you've used to write it will be invisible. It' will be converted into HTML.'Dim DSNName    'This is variable that will hold the information to connect to the databaseDim Conn    'This is the "connection" variable, it will use the DSNName variable to            'connect to the database.Dim RS      'This is the recordset variable, it will be used to access the records of the            'database which was created earlier.Dim sql     'This is a variable that will hold SQL statements.'These next three lines set the path to your database. Of course, you'll want to set'the fields to location of you own database. If you are using Brinkster, then you are'required to put the database in the db/ folder (its the only one with read/write'permissions).DSNName = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="DSNName = DSNName & Server.MapPath("db/mydb.mdb")      'The "&" symbol is a way ofDSNName = DSNName & ";PWD=mypass"                      'joining, or contatenating                                               'two strings together.'Now we are going to create our objects:Set Conn = Server.CreateObject("ADODB.Connection")Set RS = Server.CreateObject("ADODB.Recordset")Conn.Open DSNName        'Hopefully, even if you don't know SQL, the following statement will be obvious    'to you. All of the fields after the SELECT keyword correspond to the fields you    'Created in your database, the FROM keyword specifies which table that fields    'correspond to. The ID DESC statement means you are going to put your records    'in descending order; because all of your IDs will numbers (such as 1,2,3,4,5),    'putting them descending order will rearrange the records by ID so that    'the record with the highest ID is at the top and the record with lowest ID is    'at the bottom (such as 5,4,3,2,1). This will correspond to ordering your     'records from Newest to Oldest.    sql = "SELECT ID, Author, Content, Posted FROM Guestbook ORDER BY ID DESC"        RS.Open sql, Conn, 1, 1       'The RS.Open statement puts all of the records from your database into an       'array. It executes the SQL statement above. The "1, 1," at the end of the       'statement tell the database that we'll be opening it in read-only mode,        'without making any further changes to the database.       'Now we just have to loop through all the records we've created. We're going       'to put these records into a two-column table. I'm putting the HTML tags       'outside of the ASP delimeters for the sake of simplicity.       %>       <table width="100%">       <%       Do until rs.eof        'This tells the database to loop through all the                              'records in recordset.           %>           <tr>                <td align="left" valign="top">                    <p>                        <%=RS("Author")%><br>                        <%=RS("Posted")%>                    </p>                </td>                <td align="left" valign="top">                    <%=Replace(RS("Content"), vbNewline, "<br>", 1, -1, vbBinaryCompare)%>                </td>           </tr>           <%           'Those mini-%= statements above are actually equivalent to the ASP           'Response.write statement, which outputs text to the window. In this           'case, they are just writing the fields from the database. The Replace           'statement above transforms all the linebreaks the Content field into           '<br>s, so that they can be interpreted by the browser.           RS.Movenext        'This tells the database to go to the next record.                              'If you omit the RS.Movenext statement, you get an                              'infinite loop. Trust me, you don't like infinite                              'loops.       Loop       %>       </table>       <%    RS.CloseConn.Close'ALWAYS ALWAYS ALWAYS clear the objects you've created from memory by setting them'equal to nothing when you've finished with them.set conn = nothingset rs = nothing%></body></html>

That's how to display records in a database. Its important realize that *all* of the records from your database will be displayed one page; recordpaging takes a little more work, which would be overwhelming for a newbie.The code above looks intimidating, but in actuality its only 20 lines actual code with my comments taking up the bulk of it; the ASP is actually spaced out quite a bit for simplicity, the same program can be written in about 15 lines.I should also mention a disclaimer: no web developers use the RS(fieldname) method, called recordset iteration, to get contents out of a database, because while it is very simple, its also extremely inefficient (see AspFaq #2467 for an explanation); when I call information from a database, I almost never use Recordset iteration, I use the GetRows() feature to cram all of my information into a 2-dimensional array, then display it. I would have shown you the GetRows() method, but multidimensional arrays are just too much work for your first ASP project.Now, I'm going to show you how to insert records into your database. You need to have an HTML form somewhere on your page, preferably above all of your records. Just use the following HTML:

<%Option Explicit %><html><head><title>My site</title></head><body><h1>This is my site</h1><p>Write something!</p><form action="post.asp" method="post"><p>Name:<br><input type="Text" name="author"></p><p>Comment:<br><textarea rows="10" cols="50" name="Content"></textarea></p></form><hr>...

Everything else on the page will stay the same.Now, you are going to create a page called Post.asp, which will insert the contents of form on the previous page into your Guestbook table. The code for this page is actually very simple:

<% Option ExplicitDim DSNName, Conn, RS, sqlDim strAuthor, strContentstrAuthor = request("Author")     'The request keyword is used to access the                             'querystringstrContent = request("Content")   'data that you send from page to page.DSNName = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="DSNName = DSNName & Server.MapPath("db/mydb.mdb")DSNName = DSNName & ";PWD=mypass"Set Conn = Server.CreateObject("ADODB.Connection")Set RS = Server.CreateObject("ADODB.Recordset")Conn.Open DSNName    sql = "SELECT ID, Author, Content, Posted FROM Guestbook Where 0=1"    'The SQL statement above just opens the table without returning any records.    'We don't need it to return any records, because all we're doing is adding a    'new record.     RS.Open sql, Conn, 3, 3          'The "3, 3" tells the database that we'll be adding a new record to           'database.                    RS.AddNew          RS("Author") = strAuthor          RS("Content") = strContent          RS("Posted") = Now    'The now keyword inserts the date and time          RS.Update     RS.CloseConn.Closeset conn = nothingset rs = nothing'That's all there is to adding new records to the database. Now you have to 'return back to your previous page. You use the Response.redirect statement'to do that:Response.Redirect "default.asp"%>

Now you're done. You have a guestbook for your site.

I have been trying this code for a while now.... but it isnt working for me... I am thinking that it could possibly be this part of the code I may be having a problem with, but I am not sure.<code>DSNName = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="DSNName = DSNName & Server.MapPath("db/mydb.mdb") 'The "&" symbol is a way ofDSNName = DSNName & ";PWD=mypass" 'joining, or contatenating 'two strings together.'Now we are going to create our objects:Set Conn = Server.CreateObject("ADODB.Connection")Set RS = Server.CreateObject("ADODB.Recordset")Conn.Open DSNName</code>The thing is that I wrote all that code out as I was supposed to, but for some reason, it does not generate a page. And the one where you write what you want to write, there is no submit buttons, so it won't work. Still looking for help please.Also, if it helps, I am using IX Webhosting. Which my database is MySql. I am having so much trouble with this.
Link to comment
Share on other sites

I have been trying this code for a while now.... but it isnt working for me... I am thinking that it could possibly be this part of the code I may be having a problem with, but I am not sure.<code>DSNName = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="DSNName = DSNName & Server.MapPath("db/mydb.mdb")      'The "&" symbol is a way ofDSNName = DSNName & ";PWD=mypass"                      'joining, or contatenating                                              'two strings together.'Now we are going to create our objects:Set Conn = Server.CreateObject("ADODB.Connection")Set RS = Server.CreateObject("ADODB.Recordset")Conn.Open DSNName</code>The thing is that I wrote all that code out as I was supposed to, but for some reason, it does not generate a page. And the one where you write what you want to write, there is no submit buttons, so it won't work. Still looking for help please.Also, if it helps, I am using IX Webhosting. Which my database is MySql. I am having so much trouble with this.

There was nothing wrong with the code I wrote (except forgetting a submit button, oop :) ). Its just the DSNName variable thats wrong, because you're not using an MS Access database, you are using a MySQL database, so you need a different connection string.In the default.asp and post.asp pages, change this code:
DSNName = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="DSNName = DSNName & Server.MapPath("db/mydb.mdb")      'The "&" symbol is a way ofDSNName = DSNName & ";PWD=mypass"                      'joining, or contatenating                                              'two strings together.

To this:

DSNName = "PROVIDER=MSDASQL;driver={MySQL ODBC 3.51 Driver};server=localhost;option=18475;uid=your_username;pwd=your_password;database=your_database;"

Here is a ZIP file of .asp pages you need:http://www.fstdt.com/guestbook/default.zipHere is working version of the script above:http://www.fstdt.com/guestbook/default.aspHere is the SQL command you need to execute to create the structure for your table:

CREATE TABLE `guestbook` (  `ID` int(11) NOT NULL auto_increment,  `Author` varchar(25) default NULL,  `Content` text,  `Posted` datetime default NULL,  PRIMARY KEY  (`ID`)) TYPE=MyISAM;

Link to comment
Share on other sites

There was nothing wrong with the code I wrote (except forgetting a submit button, oop  :) ). Its just the DSNName variable thats wrong, because you're not using an MS Access database, you are using a MySQL database, so you need a different connection string.In the default.asp and post.asp pages, change this code:
DSNName = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="DSNName = DSNName & Server.MapPath("db/mydb.mdb")      'The "&" symbol is a way ofDSNName = DSNName & ";PWD=mypass"                      'joining, or contatenating                                              'two strings together.

To this:

DSNName = "PROVIDER=MSDASQL;driver={MySQL ODBC 3.51 Driver};server=localhost;option=18475;uid=your_username;pwd=your_password;database=your_database;"

Here is a ZIP file of .asp pages you need:http://www.fstdt.com/guestbook/default.zipHere is working version of the script above:http://www.fstdt.com/guestbook/default.aspHere is the SQL command you need to execute to create the structure for your table:

CREATE TABLE `guestbook` (  `ID` int(11) NOT NULL auto_increment,  `Author` varchar(25) default NULL,  `Content` text,  `Posted` datetime default NULL,  PRIMARY KEY  (`ID`)) TYPE=MyISAM;

Thanks, that's where I thought that the problem was. I appreciate it.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...