The first thing you have to do when working with PHP at any time, is tell the browser that you are going to work with PHP. In order to do this you will use the "PHP Tags".
Html cannot be used directly inside of the "PHP Tags" unless you use something called an echo or a print. For now, sense we are using the K.I.S.S. method, keep the form information from above OUTSIDE of the "PHP Tags", and all of your PHP Code, INSIDE of the "PHP Tags".
The <?php tells the browser you are going to start coding in PHP, and the ?> tells the browser you are done coding in php.
The first thing we will need to do in order for the register to work what so ever is connect to your database. Almost every server uses mysql to actually store account information, unless you are using 5165 or something that is done by text files, which I will cover later on. To connect to a mysql database, you will need to do the following.
PHP Code:
<?php
$connection = mysql_connect("localhost", "db username", "db password");
$database = mysql_select_db("db name");
?>
As you can see, we have our programming inside of our "PHP Tags" still. The dollar sign $ is stating that we are accessing a variable, in this case we are setting it equal to the mysql connection and another variable to the mysql database. You can name the $connection or the $database what ever you want, but I like to keep it simple and name it what its used for.
The mysql_connect is a built in function in PHP used to connect to a mysql database. It has a total of 3 arguments, each one is split out by a comma. The first argument (or "localhost" above) is the host your connection is running on. If you are running your server on the computer your running your website off of, it will be localhost for you as well. The second argument (or "db username") is the username that you use to login to mysql. In most cases the default is root. The third argument (or "db password") is the password that you use to login to mysql. Also if you notice, each line its with a ; to tell the browser that you are ending that line of php code and are starting a new line. Without the ; you may come across many errors.
In the $database variable you can see that we are setting it to mysql_select_db, which basically does exactly what it says, it selects the database inside mysql that you are going to be using. This will be your copro, coemu, or w/e you name your database inside of mysql. And that will end with the ; as well. So the most common way you will see this is the following.
PHP Code:
<?php
$conn = mysql_connect("localhost", "root", "mysqlpass");
$db = mysql_select_db("coproj");
?>
Now that we have got PHP to connect to mysql, lets start accessing the form information we pass across from the HTML form we did earlier.
PHP Code:
<?php
$conn = mysql_connect("localhost", "root", "mysqlpass");
$db = mysql_select_db("coproj");
if(isset($_POST['submit']))
{
if(isset($_POST['pass']) && $_POST['pass'] != "")
{
if($_POST['pass'] == $_POST['pass2'])
{
if(strlen($_POST['pass']) > 3 && strlen($_POST['pass']) < 32)
{
$sql = "INSERT INTO `tablename` (Table1, Table2) VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$sql = mysql_query($sql);
echo "Registered Successfully";
} else {
die("Your password must be between 3 and 32 characters.");
}
} else {
die("Your passwords do not match.");
}
} else {
die("Please enter in a password.");
}
}
?>
I know that looks like a lot and its im not expecting you to understand it all at once. I will break down each and ever part of it for you so u can understand it a little more. As we discussed earlier, they are still inside the "PHP Tags", and we still have our connection to MySQL up at the top. After the connection you will see the following.
PHP Code:
if(isset($_POST['submit']))
{
}
This statement is basically saying, If the $_POST['submit'] variable is set (hints the isset function), then do the following inside the brackets. If $_POST['submit'] is set (submit is the variable passed by our html form we created earlier), then that means that the form has been submitted.
HTML Code:
<input type="submit" name="submit" value="Submit" />
If you notice the name="submit" is why we are using $_POST['submit']. So if you use the following.
HTML Code:
<input type="submit" name="processform" value="Submit" />
Then in order to to check if the form was submitted, we would use the following.
PHP Code:
if(isset($_POST['processform']))
{
}
If it hasnt been submitted, then we dont need to do anything at all. So what you should get out of this statement is for 1, to check if something is true, you use the if statement, and the $_POST array contains all the form information using the method "post". Remember earlier when we were talking about post and get?
This line of HTML code is where we selected the method post.
HTML Code:
<form method="post" action="">
Well this is where it comes in handy. If you selected the method to be get earlier, we would be using $_GET['submit'] for the information.
The next line inside of code checks whether or not the password field has been used or not.
PHP Code:
if(isset($_POST['pass']) && $_POST['pass'] != "")
{
}
As you can see, the code up top checks to see if the variable pass is set and to check if its not equal to nothing. So if the pass variable is set and its not equal to nothing, it basically means they have entered in a password, so if they entered in a password, it will continue with the code inside of the brackets.
The next line of code will check whether or not the password field and password 2 fields are equal to each other.
PHP Code:
if($_POST['pass'] == $_POST['pass2'])
{
}
As you can see, instead of using = we use the == operator. The = operator is to set information to the variable, where the == operator is used to check if its equal or not equal to (in other words a boolean). So the above is checking if pass and pass2 are equal to each other.
The next line of code checks the length of the string for password to check if its inside the length you want them.
PHP Code:
if(strlen($_POST['pass']) > 3 && strlen($_POST['pass']) < 32)
{
}
The strlen function is used to get the number of characters inside of a string. In this situation we are basically saying if the string inside $_POST['pass'] is greater than 3 AND (the && operator is basically saying AND) check to make sure its less than 32 characters. If its between both of those characters, it will continue the line of code inside the brackets.
Inside of the brackets you will see the following.
PHP Code:
$sql = "INSERT INTO `tablename` (Table1, Table2) VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$sql = mysql_query($sql);
echo "Registered Successfully";
The first $sql is setting a string to the variable for later use. As you can see, it is saying INSERT INTO `tablename` which would be the table you want to insert the information into. Then the (Table1, Table2) are the columns or fields inside of the table that you want the information inserted into. Then the VALUES operator to tell mysql that we are inserting the following values. ('".$_POST['username']."', '".$_POST['pass']."') is the values we are inserting into Table1 and Table2. Notice that the first table (Table1) will be inserted with the Username information. Due to them being both in the same slot, and the Table2 will be inserted with the password information because they are both in the 2nd slot.
The next line is setting the variable $sql to the mysql_query of $sql. This is actually executing the query to insert the information into mysql.
The line after that is the echo, this will basically display the text Registered Successfully onto your website.
So the hole register page should look like this.
PHP Code:
<form method="post" action="">
Username: <input type="text" name="username" value="" /><br/>
Password: <input type="password" name="pass" value="" /><br/>
Repeat Password: <input type="password" name="pass2" value=""/><br/>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$conn = mysql_connect("localhost", "root", "mysqlpass");
$db = mysql_select_db("coproj");
if(isset($_POST['submit']))
{
if(isset($_POST['pass']) && $_POST['pass'] != "")
{
if($_POST['pass'] == $_POST['pass2'])
{
if(strlen($_POST['pass']) > 3 && strlen($_POST['pass']) < 32)
{
$sql = "INSERT INTO `tablename` (Table1, Table2) VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$sql = mysql_query($sql);
echo "Registered Successfully";
} else {
die("Your password must be between 3 and 32 characters.");
}
} else {
die("Your passwords do not match.");
}
} else {
die("Please enter in a password.");
}
}
?>