insert new user

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

insert new user

Post by jayson.ph »

Hi all,

i am, now creating for insert new user, like username and password. and now i have no idea in how to code a correct syntax in php that when i input different password it well be ignore and must be the password are equal. please help me a little idea. take a look code below.

Code: Select all

	<div class = "octagon_class0001010001">
		<form method = "POST">
		<div class = "octagon_class0001010002">
			<div id = "octagon_id0001010003">	
			User Teype:
			<select name = "usertype" style = " width:250px; height:20px; float:right;">
				<option>User</option>
				<option>Administrator</option>
			</select></div>
			<div id = "octagon_id0001010003">User Name:<input type = "text" name = "username" style = " width:250px; height:15px;float:right;"/></div>
			<div id = "octagon_id0001010003">User Password:<input type = "password" name = "password" style = " width:250px; height:15px;float:right;"/></div>
			<div id = "octagon_id0001010003">Re-Type Password:<input type = "password" name = "re_password" style = " width:250px; height:15px;float:right;"/></div>
			<div id = "octagon_id0001010003"><input type = "submit" name = "add_user_sub" value = "add user" style = " width:100px; height:25px;float:right;"/></div>
		</div>
		</form>
	</div>
</div>
	<?php
		include "../config/jph-config.php";
		if(isset($_POST['add_user_sub']))
		{
			$_my_user_type = $_POST['usertype'];
			$_my_user_name = $_POST['username'];
			$_my_password = $_POST['password'];
			$_my_re_password = $_POST['re_password'];
		mysql_query("INSERT INTO `tbl_user`(usr_type,usr_name,usr_pass,re_usr_pass) VALUES ('$_my_user_type','$_my_user_name','$_my_password','$_my_re_password')");
		}

	?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: insert new user

Post by social_experiment »

jayson.ph wrote:i input different password it well be ignore and must be the password are equal.
Are you referring to validation checks?

Basic validation works by comparison of the two values;

Code: Select all

<?php
 $pass1 = $_POST['password'];
 $pass2 = $_POST['password2'];

 // if pass1's value is not equal to pass2's value
 if ($pass1 != $pass2) {}

?>
This url will shed more ligth on comparison operators;
http://md.php.net/manual/en/language.op ... arison.php
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: insert new user

Post by Celauran »

No need to store the password twice in the database. Also, don't store passwords in plain text.
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: insert new user

Post by jayson.ph »

Hi all,

>>social_experiment: and yes im going to validate. and Thank you for your great sample code.
>>Celauran: please help me, share us you idea to work my work great.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: insert new user

Post by Celauran »

jayson.ph wrote:Celauran: please help me, share us you idea to work my work great.
Threw this together quickly. Can't guarantee it's 100% bug free. Should at least point you in the general direction.

Code: Select all

<?php

// Stop using mysql_ already!
$sql = new PDO('mysql:host=localhost;dbname=foo;', 'username', 'password');

$errors = array();
if (isset($_POST['add_user_sub']))
{
    /**
     *  This is all kind of pointless
    $_my_user_type   = $_POST['usertype'];
    $_my_user_name   = $_POST['username'];
    $_my_password    = $_POST['password'];
    $_my_re_password = $_POST['re_password'];
     */

    // Check that username isnt' already in use
    $query = "SELECT COUNT(usr_name)
              FROM tbl_user
              WHERE usr_name = :user";
    $stmt = $sql->prepare($query);
    $stmt->execute(array(':user' => $_POST['username']));
    $count = $stmt->fetchColumn();

    if ($count)
    {
        $errors[] = "That username is already in use.";
    }

    // Check that passwords match
    if ($_POST['password'] != $_POST['re_password'])
    {
        $errors[] = "Passwords do not match.";
    }
}

// No errros, so we're ready to create the user
if (isset($_POST['add_user_sub']) && empty($errors))
{
    // Hash the password. Make sure the password field in your database is long enough.
    $password = crypt($_POST['password'], '$2a$12$' . substr(md5(microtime()), 0, 22));
    $query = "INSERT INTO tbl_user (usr_type, usr_name, usr_pass)
              VALUES (:type, :username, :password)";
    $stmt = $sql->prepare($query);
    $stmt->execute(array(
        ':type' => $_POST['usr_type'],
        ':username' => $_POST['username'],
        ':password' => $password,
    ));
}
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: insert new user

Post by jayson.ph »

Good Evening guys,

And Thanks a lot 'Celauran' you really get my point. thank you for being their. and i use this sample code above and i try to learn it.


Thank you.
Post Reply