PHP use mySQL to check existing username -
i have been following tutorial building simple registration / login script.
http://forum.codecall.net/topic/69771-creating-a-simple-yet-secured-loginregistration-with-php5/
i new php, have lot of experience using c++, thought transitions wouldn't hard, need figure out syntax. did very quick introduction mysql @ university, thought lot easier use mysql check existing username when user has registered, though knowledge isn't good. thought work;
select username codecalltut username = username;
would work? selecting username database codecalltut , checks see if username being inputted username? if correct don't know how include in php.
i've tried using
$qry = "select username codecalltut username = username;"
but syntax error when moves next statement.
<?php $qry = "select username codecalltut username = username;" //if register button clicked. } else { $usr = new users; //create new instance of class users $usr->storeformvalues( $_post ); //store form values //if entered password match confirm password register him if( $_post['password'] == $_post['conpassword'] ) { echo $usr->register($_post); } else { //if not must enter same password confirm box. echo "password , confirm password not match"; } } ?>
this query used construct database:
create database `codecalltut` default character set latin1 collate latin1_swedish_ci; use `codecalltut`; create table if not exists `users` ( `userid` int(11) not null auto_increment, `username` varchar(50) not null, `password` varbinary(250) not null, primary key (`userid`,`username`) ) engine=innodb default charset=latin1 auto_increment=6 ;
this html code when user clicks "register"
<li class="buttons"> <input type="submit" name="register" value="register" /> <input type="button" name="cancel" value="cancel" onclick="location.href='index.php'" /> </li>
your html form registration
<form action='' method='post'> <input type='text' name='username' /> <input type='password' name='password' /> <input type='password' name='re-password' /> <input type='submit' name='submit' /> </form>
your php code
if($_post){ if(empty($_post['username']) && empty($_post['password']) && empty($_post['re-password'])) { echo 'please enter fields'; }else { $username = $_post['username']; $password = $_post['password']; $re_password = $_post['re-password']; if($password !== $re_password){ echo 'both passwords not match'; }else { $db_name = $db_user = $db_pass = $conn = new pdo('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx', array( pdo::attr_persistent => true ) ); $stmt = $conn->prepare("select username,password users username = ? , password = ?"); $stmt->execute(array($username, $password)); if($stmt->rowcount() === 0 ) { $stmt = $conn->prepare("insert users (username, password) values (?,?)"); $stmt->execute(array($username, $password)); if($stmt->rowcount() ===1){ echo 'registration complete'; }else { echo 'sorry, unknown error: please try again later'; } }else { echo 'sorry, username '.$username.' exists'; } } } }
Comments
Post a Comment