So far, you've learned a lot about PHP. It's time to write a small program practicing.
User Registration System
Write a index.php page with user name and password form, post submit to check.php, output username and "username + password" MD5 value
Local set up a MySQL database, named Segmentfault, set up a table user, field username password will be on the title check.php received the content deposited
Let's finish the HTML section first:
Note that HTML5 's support for forms is much better than previous versions of HTML, and can be used to indicate various types directly. For example, the establishment of type= "email", will verify that the user submitted the e-mail address is legitimate.
And then the check.php.
Here is the PHP section, which embeds HTML directly, which is the advantage of PHP:
<?php
$username = htmlspecialchars ($_post["name"));
echo $username;
? >
</p>
<p> your "Mailbox + password" MD5 value is:
<?php
$passphrase = Htmlspecialchars ($_post[" Passphrase "]);
$MD 5sum = MD5 ($username. $passphrase);
echo $MD 5sum;
Note that we use the htmlspecialchars to avoid the user from filling in strange things.
Then the database operation, we use mysqli, (MySQL has been discarded, now recommended to use MYSQLI, of course, you can also use PDO. )
First we indicate some information about the database:
$db _server = "localhost";
$db _user = "Db_user";
$db _pass = "password";
$db _name = "Segmentfault";
Connect to the database and make sure the connection is normal:
$conn = new Mysqli ($db _server, $db _user, $db _pass, $db _name);
if (Mysqli_connect_errno ()) {
trigger_error ("Database Connection failed:".) Mysqli_connect_error (), e_user_error);
Create a table using SQL statements
Copy Code code as follows:
$sql = "CREATE TABLE User (username char (140), password char (140))";
Add a record
Copy Code code as follows:
Mysqli_query ($conn, "INSERT into user (username, password) VALUES ($username, $MD 5sum)");
Handwriting sql is not very cool, in practical applications, the framework will provide you with a variety of convenience.
To close the database:
Mysqli_close ($conn);
? >
</p>
</body>
Well, we've successfully written a small application and incidentally learned how to access the MySQL database. That's it for today.