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!
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.
>>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.
<?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,
));
}