PHPSuperBlog.com

MD5 – insert an encrypted string or password to the MySQL database with HTML form and PHP

Editor’s note: In a login system, there are user names and passwords stored in the MySQL database. The passwords are normally stored as strings inside the database. To have a more security way to store the passwords in the database, it is better to store an encrypted version of the password, therefore only the password creator knows what the password is.

MD5 is the encryption technology I like to use because it is very easy to implement and it encrypts a string into a 32-character strings, for reference what MD5 really is, please check Wikipedia-MD5.

Here I have created an example to show how to store a string with PHP function “md5( )” encrypted.

I have created a database with a table named “test” with a field named “password”.
I have created a HTML file with a HTML form called “insertform.html”.
I have created a php file called “insert.php”.

Here are the codes:

For insertform.html:

A HTML form with 1 text field and one submit button create here. User has to input the password into the text field named “userpassword”. When the submit button is clicked, the form will direct us to the “insert.php” file.

<form action="insert.php" method="post">
Insert Password:
<input name="userpassword" type="text" />
<input name="Submit" type="submit" value="insert password" />
</form>

The HTML Form:

Insert Password:

For insert.php:

First we have to connect to database.

mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed");
mysql_select_db("DataBase Name")or die("Connection Failed");

Then we create a PHP variable to hold the value in the form of MD5 hash by using the md5( ) function from the text field “userpassword” of the HTML form.

$password = md5($_POST['userpassword']);

Now we create the query for inserting the MD5 hash password to the table “test”.

$query = "insert into test values ('$password')";

We then run the query with the “mysql_query( )” PHP function and to check whether the MD5 hash password has inserted successfully, I have put an “if” condition there to echo out the result.

if(mysql_query($query)){
echo "inserted";}
else{
echo "fail";}

All together for insert.php is:

<?php
mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed");
mysql_select_db("DataBase Name")or die("Connection Failed");
$password = md5($_POST['userpassword']);
$query = "insert into test values ('$password')";
if(mysql_query($query)){
echo "inserted";}
else{
echo "fail";}
?>
VN:F [1.9.3_1094]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
MD5 - insert an encrypted string or password to the MySQL database with HTML form and PHP, 10.0 out of 10 based on 1 rating

Similar Posts:

Leave a comment