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.
1 2 3 4 5 | <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.
1 2 | 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.
1 2 3 4 | if(mysql_query($query)){ echo "inserted";} else{ echo "fail";} |
All together for insert.php is:
1 2 3 4 5 6 7 8 9 10 | <?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";} ?> |





sherwin said,
hi, i am sherwin. i have questions about md5 password.
im using fedora 15 linux so my database base on linux,
so now i want to create the md5 pass generate, now i have
ready php to generate md5 or hash , when i generate md5 or hash into mySql cant change.
can you gave me exactly idea, or source. thank you
Shek said,
can you give more specific on the issue, so that I could help you? specially on the part "generate md5 or hash into mySql cant change", do you mean you cannot change the MD5 hash after you insert into the MySQL?
Add A Comment