MD5 – insert an encrypted string or password to the MySQL database with HTML form and PHP
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";} ?>
Similar Posts:
- Insert values into MySQL database with HTML form and PHP
- Delete records from MySQL database with HTML form and PHP
- Insert multiple lines from textarea of HTML Form into MySQL database with PHP
- Delete table from MySQL database with HTML form and PHP
- Empty a table in MySQL database with HTML form and PHP






