I keep getting more than 1 message popping up on my registration form. Could anyone please help me understand what I'm doing wrong? I keep looking at it and I'm not sure where I've gone wrong.
Register form
<?php include_once("config.php");?>
<?php if( !(isset( $_POST['register']))) { ?>
<!DOCTYPE html>
<html>
<head>
<title>Register </title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header id="head" >
<p>Please Register</p>
<p><a href="register.php"><span id="register">Register</span></a></p>
</header>
<div id="main-wrapper">
<div id="register-wrapper">
<form method="post">
<ul>
<li>
<label for="usn">Username : </label>
<input type="text" id="usn" maxlength="30" required autofocus name="username" />
</li>
<li>
<label for="passwd">Password : </label>
<input type="password" id="passwd" maxlength="30" required name="password" />
</li>
<li>
<label for="conpasswd">Confirm Password : </label>
<input type="password" id="conpasswd" maxlength="30" required name="conpassword" />
</li>
<li class="buttons">
<input type="submit" name="register" value="Register" />
<input type="button" name="cancel" value="Cancel" onclick="location.href='login.php'" />
</li>
</ul>
</form>
</div>
</div>
</body>
</html>
<?php
} else {
$usr = new Users;
$usr->storeFormValues( $_POST );
if( $_POST['password'] == $_POST['conpassword'] ) {
echo $usr->register($_POST);
} else {
echo "Password and Confirm password not match";
exit;
}
$username = ($_POST['username']);
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sthandler = $con->prepare("SELECT username FROM users WHERE username = :username");
$sthandler->execute(array(':username'=>$username));
if ( $sthandler->rowCount() > 0 ) {
echo ('<br> Sorry, the username '.$_POST['username'].' is already in use.');
}else{
echo "username is free";
}
}
?>
Register function page
public function register() {
$correct = false;
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO users(username, password) VALUES(:username, :password)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
$stmt->execute();
return "Registration Successful <br/> <a href='index.php'>Login Now</a>";
}catch( PDOException $e ) {
return $e->getMessage();
}
}
This is the message I keep getting. I need 1 or the other to display. Not both at the same time. PLUS, this is the 1st time I try and register j123 so I'm not sure how I get my username to display that the username is really already used.
Registration Successful
Sorry, the username j123 is already in use.
It's simple, look:
Change it:
if ( $sthandler->rowCount() > 0 ) {
echo ('<br> Sorry, the username '.$_POST['username'].' is already in use.');
}else{
echo "username is free";
}
For it:
if ( $sthandler->rowCount() > 0 ) {
echo ('<br> Sorry, the username '.$_POST['username'].' is already in use.'); exit();
}else{
echo "username is free";
}
Only add "exit();" after the echo when the count return more than 0. This command will stop the script.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.