ASP Database Inserts

[Total: 0    Average: 0/5]

Inserting Information Into a Database

Today we will be looking at how we can insert data into a database. We will be starting a project to build a username & password protected web page. The first part is entering a user into our database.

We will start by designing an Access database that will hold the information about our users. Please note that if you want more information in your database than we are using, you can always add more fields. For now, we will only be using 3 fields in our users table. Start by opening Access and creating a new database. Call it whatever you want. Then double click on “Create Table in Design View.” One thing we must keep in mind when designing this table is that we don’t want any user to have the same username as another. Therefore, we create 2 primary keys. One for the userID (which is an autonumber field) and one for the actual username.

The first field is the userID field and the datatype will be set to AutoNumber. This automatically increments each entry by one number. The second field is the username field and the datatype will be text. The third field is the password field and the datatype will be text also. To create both the userID and username fields as primary keys, click on the area where you see the key and drag down so both rows are highlighted in this key column. Right click and select “Primary Key.” Save this table with the name users.

Now that we have our users table set up in our database, we will have to add the info to it. This is where it gets fun. Before proceeding, make sure that you have a connection set up to your database. If you don’t know how to do it, refer to our tutorial on connecting to a database.

Creating our createuser.asp page

We need to create 2 pages to insert data into our database. The first will be a regular form page, but still ASP to report any errors. The second page will be an ASP page that will actually do the work of inserting the data into our database. Let’s start by creating our form. Name this page createuser.asp.

You might notice something new above. We used the request object that we talked about in our introduction. The reason for this is so that if we try to register someone who requests a username that is already in use, the insertuser.asp page will redirect us to this page and ask us to try again. Otherwise, if the user was successfully added, it will tell us that.

Creating our insertuser.asp page

Now we need to create the page that will take the form data and enter it into the database. Name this page insertuser.asp.

That’s all there is to it. Now you know how to create a form to add information into your database. I’m sure you can think of many more fields to add in here such as first name, last name, phone number and more to get a complete listing of your users. But for now, this functions just fine in order to show you how to create this application. See ya next time!

~Geoff Swartz

Times Viewed: 24

Leave a Reply

Your email address will not be published. Required fields are marked *