Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,800 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Understanding forms
- Adding required fields and placeholders
- Accepting multiple entries
- Limiting uploads
- Handling focus changes
- Validating with regular expressions
- Working with older browsers
- Building jQuery validation
- Using server-side validation
- Sanitizing form input
- Uploading files
- Sending form data to a database
Skill Level Intermediate
Sometimes, you don't want to send the information to an email address. It can be unreliable and it's not easy to aggregate the data. In that case, it's better to push the information to a database. Most servers come with some implementation of the popular My SQL database. And it's pretty easy to use since the commands are fairly readable. This isn't designed to be a full database course. We'll just be talking about form implementation. So for more information, make sure you check out PHP with my SQL Essential Training, and for more SQL information, check out My SQL Essential Training.
So, we've got a do a little bit of set-up. You're going to need to get credentials that give you access to the database. That usually involves creating the database, which you can do either through SSH or your host control panel. There's a lot of different control panels, and how you get to this varies from server to server. So I'm going to assume you already created a database. If you want to learn how to do that in some popular control panels, like C panel, Plesk or One in One, check out my course on managing a hosted website. You're going to end up with four pieces of data. The first one is the host name and here I place it in a bearable called host.
Some databases are hosted on servers other than your main domain. So the host name is going to be the url of the website with the database. If it's hosted in the same server as your site, you can use the keyword localhost. Databases are accessed with a username and password combination, so you'll need both of these in order to be able to post your data. I've created a variable called user and password here to store that information. Finally, you'll need the name of the database. I've called mine storeform. It's customary to place these on a separate PHP file and include them in your current docment.
The reason for this, is that you're probably going to want to use these on several files. So I've created this file, and it's called log_formdb.php. You can call yours wherever you want. I've placed this file on my server in the same folder as all my other files. Now, I won't be including this file in the exercise files for security reasons, but it's easy enough for you to create your own. In addition to creating a database, you'll also need to create a table. Think of a table as spreadsheets where your data is stored. You usually create tables with a control panel, like PHP MyAdmin, SSH or some other dedicated program. I'm going to show you how to do with PHP MyAdmin.
This my server's administration screen, and I've already created the database. But I need to create the table. So I'm going to click on this icon right here, and it will launch the PHP MyAdmin program. It's really common to see that program installed in a bunch of different hosts. So from here, I can hit create table, and in here I can give it a name. I'm going to call this form_info and then I need to create a number of fields. Now at first this program gives me two columns. The first column is usually a unique ID for the table.
So I'm going to call this form info_id. And I need to tell it what type of file this is going to be. Its already prechosen integer for me and that's going to work fine. This is going to be just an integer number that's going to be 11 units maximum. That's a lot of data base entries. Now you want to make this file the primary index, every database needs at least one primary index. You also want to check this check box called auto increment as you create new entries into this stable the ID number will automatically increment to show you've got new items on your table. The next field I'm going to create is a timestamp.
I'm going to call this one, forinfo_ts for timestamp, and this one's going to be a date and time. Notice there's a date area right here, and right over here under date and time there's one called date time, that's the one I'm going to use. You don't need to specify the length of a date time, there's a default length for that type of field. I know I'm going to need some additional columns, so I can save this or add a number of other columns. I'm going to need six additional columns, so I'll type that in, and select go. So now you can see there's additional columns, now it's switched to a horizontal layout here, but, it's pretty much the same as we saw before. I'm going to make these variables the same as the names in my PHP files. They don't really have to be, but it's just more convenient. Next one's going to be my name.
Obviously that's going to store my name. Usually when you store textual information, you can choose a type call varchar, it means a variable amount of characters. And after that, you tell it how many characters this should have. So for a name, since our name's going to be a first name and last name, I'm going to guess about 70 characters will be the same to hold any type of name. Now next, I'm going to enter my password. My password is also going to be a variable character. This one's going to be 60 characters long. That's because I'm going to take whatever the user types and then encrypt and add a sol to it. We'll talk more about that later.
60 characters should be enough to hold that. I'll add a section for my comments. Now my comments needs to be really long, so I'm going to choose from here, this type called, Text. I don't have to choose length or values when I enter a type of text. Next, I'm going to type in the reference, that's going to be another varchar, and I'm going to hold 15 characters. Then it'll be Favorite Music and again a varchar, 15 characters. Finally, I need to make a request type, and again, varchar 15 characters long. So, that should do it for my table.
I'm going to hit Save and it will create my database. So, there it is. And I can come over here and click on the database, and if I have any values, I can check on this browse part. Right now, this table's empty, so it's not letting me click on that. You can see the structure is complete, it looks really good, and if I want to, I can start putting values into this database. You can do that by clicking Insert here, but we're going to fill it out with our form. So, working with tables requires a little bit of set up on your part. And you are going to need to check out how your host handles database creation.
Once you have a database, you have the ability to store a number of records, and will later be able to query the database for any type of information you need about your users
Sign up for a Premium Membership to download courses for Internet-free viewing.
Watch offline with your iOS, Android, or desktop app.Start Your Free Trial
After signing up, download the course here or from the iOS/Android App.