Jump to content

PHP MD5 Password Encryption and DB Entry Problem - 03.19.12


mrjap1

Recommended Posts

Hello Everyone,

 

I have built a simple registration form shown below and Iam trying to get the users to have their passwords encrypted and then entered into my database. I am attempting to use md5 encryption. I have also attached the database connection script.

 

My goal is when I check my database, I want to see the following: ( id, name, username, encrypted password )

 

The issue I have is that the form does not process completely. All I get this error (Error: Unknown column 'd8578edf8458ce06fbc5bb76a58c5ca4' in 'field list' ).

 

Could some tell me or show me " What is it that needs to be corrected either in my Code or SQL insert and /or my Variables" to make this work correctly. I know that its probably a very, very simple fix... Im just stuck at this point.

 

I really appreciate your help.

 

Thx,

mrjap1

 

<?php
error_reporting(0);
if($_POST['submit'])
{ //Begining of full IF Statment
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
// Encrypt Pasword
$enc_password = md5($password);
//$enc_password2 = md5($confirm_password);


// Confirm All feild were filled out when submit button was pressed
if($name && $username && $password && $confirm_password) 
{
// Confirm that the NAME that you used is NOT greater than 30 characters	 
	 if(strlen($name)>24)
	 {
	 echo "<h2><center>YOUR NAME IS TOO LONG!!!!</center></h2><br>";
	 }
// Confirm that the USERNAME that you used is NOT greater than 10 characters	 	
	if(strlen($username)>10)
	 {
	 echo "<h2><center>YOUR USERNAME IS TOO LONG!!!!</center></h2><br>";
	 }
	 else {

// Confirm that the PASSWORD that you used MATCH & Between 6 and 15 characters	 
		if(strlen($password)>10 || strlen($password)<6)
		 {
		 echo "<h2><center>YOUR PASSWORD MUST BE BETWEEN 6 and 15 CHARACTERS!!!!</center></h2><br>";
		 }
		if($password == $confirm_password)
		{
		// Database Connection required
		require "db_conncect.php";
		// We Now connect to the Dabase and insert the Form input details
		//------- ### ENTERING ALL INFORMATION INTO THE DATABASE BELOW ### --------// 


// 1. Create a database connection
$con = mysql_connect("localhost","root",""); // <-- THIS IS WHERE YOU " CAN CHANGE " THE USERNAME IS "root", PASSWORD IS "" ONLY.

if (!$con) {
  die('Database connection failed could not connect: ' . mysql_error());
  }
  
// 2. Select a database to use
$db_select = mysql_select_db("registernow_2012",$con); // <-- THE "registernow_2012" IS THE NAME OF THE DATABASE.
if (!$db_select) {
  die('Database selection failed could not connect: ' . mysql_error());
}

mysql_select_db("registernow_2012", $con); // <-- THE "registernow_2012" IS THE NAME OF THE DATABASE TO BE CONNECTED.

// <-- THE `registernow_2012` IS THE NAME OF THE DATABASE TO BE CONNECTED.... `visitors` IS THE TABLE WITH ALL THE FIELDS WITHI IN THE DATABASE.


$sql="INSERT INTO `registernow_2012`.`users` (
`id` ,
`name` ,
`username` ,
`$enc_password` ,
`confirm_password`
)
VALUES (
NULL , '$_POST[name]', '$_POST[username]', '[$enc_password]', '$_POST[confirm_password]'
)";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
}

// 3. Close Connection
mysql_close($con);

header("Location: index.php");  // <-- THIS IS WHERE YOU CAN CHANGE THE "Location: Thank you / Index page" of the THANK YOU PAGE.		

		}
	else 
	{
	echo "<h2><center>PASSWORDS MUST MATCH!!!!!</center></h2><br>";
	}	

	 }

	//echo "<h2><center>WORKING!!!!</center></h2>";
}	
else echo "<h2><center>ALL FEILDS MUST BE COMPLETED</center></h2>";

} //Ending of full IF Statment
?>

<!DOCTYPE html>
<html lang='en'>
<head>
<title>THE FORM MY WAY NOW</title>
</head>
<div id='centerstage'>
<form name="myform" action="workingitoutproperly.php" method="POST">
<p>
<label>Name</label><br>
<input type='text' name='name' value=''><br>
<label>UserName</label><br>
<input type='text' name='username' value=''><br>
<label>Password</label><br>
<input type='password' name='password' value=''><br>
<label>Re-Enter Password</label><br>
<input type='password' name='confirm_password' value=''><br>
<br>
<input type='submit' name='submit' value='REGISTER NOW!!'>
</p>
</form>
</div>
</html>


17810_.php

Link to comment
Share on other sites

The error is pretty obvious. It appears you are using the hashed value (it is not encryption) as the field name for the password. Also, in the future, it would be appreciated to just post the relevant code for your problem, not the whole script.

 

Here is your current query:

$sql="INSERT INTO `registernow_2012`.`users` (
`id` ,
`name` ,
`username` ,
`$enc_password` ,
`confirm_password`
)
VALUES (
NULL , '$_POST[name]', '$_POST[username]', '[$enc_password]', '$_POST[confirm_password]'
)";

 

You are using "$enc_password" as a field name. You also have a field for "confirm_password". That doesn't make sense since that is typically used as the 2nd entry field for the password and is only used for validation purposes and is not stored. As you can see you have put the VALUE of the hashed value in the field list and not in the values list. You need to use an appropriate field to store the value - I will assume "password".

 

So, you need to fix. Plus, you don't need to include the "id" in the field list if you are just going to pass a null value.

$sql = "INSERT INTO `registernow_2012`.`users`
            (`name`, `username`, `confirm_password`)
        VALUES
            ('{$_POST['name']}', '{$_POST['username']}', '{$enc_password}')";

 

Lastly, the above script is wide-open to SQL Injection. You need to validate/sanitize the values appropriately based upon the type of data you expect and the storage type. And, you should look into using a hash that uses a salt - plenty of posts on this forum and elsewhere on how to do that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.