PHP Logic for Username NOT WORKING
Error I'm getting: Whether I try a valid username or password, it always says "Sorry, username already exists" . Is something wrong with my logic, specifically with this code block:
$q = $conn->query("SELECT uname FROM Student WHERE uname= $aRegUsr ");
$stmt = $conn->prepare($q);
$stmt->fetch($q);
if($stmt > 0){ #if there are 1 or more users with enter username, deny.
dbcred.php
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'databasename';
?>
PHP:
<?php
#Login Details
require_once('dbcred.php');
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$aRegUsr = $_POS开发者_如何学CT['regduser'];
#Check for Existing User
$q = $conn->query("SELECT uname FROM Student WHERE uname= $aRegUsr ");
$stmt = $conn->prepare($q);
$stmt->fetch($q);
if($stmt > 0){ #if find that username in use, deny ability to register, else{ALLOW}
echo "Sorry, username already exists";
}
else{
echo "Success";
}
?>
HTML
<form action="inc/check_regUsr.php" method="post" id="userLogon">
<div class="field required">
Username: <input type="text" name="regduser" tabindex="1" /><br />
</div>
<div class="field required">
Password: <input type="password" name="regdpass" tabindex="2" /><br />
</div>
<input type="submit" name="submitUser" />
</form>
DB Table: Student
Are you trying to create a user registration or user authentication script? I don't believe your actual purpose is clearly outlined to help provide an effective solution.
"Whether I try a valid username or password" indicates user authentication, whereas "if($stmt > 0){ #if there are 1 or more users with enter username, deny." indicates user registration.
EDIT: Based off of your updated requirements, user authentication can get a bit complicated depending on your deployment's security. Included below is sample code based off of your base code. Please note that you may want to find a pre-built script and modify it for your needs as this may serve your purpose better.
<?php #Login Details
require_once('dbcred.php');
#Variables
$error = '';
$auth = 0;
$aRegUsr = $_POST['regduser']; #Check for Existing User
$aRegPass = $_POST['regdpass']; #Check for Existing User
#DB stuff
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$sql = "SELECT uname,password FROM Student WHERE uname= $aRegUsr ";
#Dataset stuff
foreach($conn->query($sql) as $row){
if($row['password'] != $aRegPass){
$error = 'Invalid Password';
$auth = 0; #If $auth = 0 then user is logged in
}else{
$auth = 1; #If $auth = 1 then user is logged in
}
}
$conn = null;
?>
Im not sure as i never used this functionality but from what I understood from here: http://php.net/manual/en/pdostatement.fetch.php fetch return the results so you would actually have to check and see if the count of the results is greater than 0. Check fetchAll http://php.net/manual/en/pdostatement.fetchall.php it should be something like this:
$results = $stmt->fetchAll();
if(count($results)>0)
{
...
Your code is incorrect. I assume your logic in if ($stmt > 0)
is to test for if there is already a user existing with that name.
Use this instead -
<?php
#Check for Existing User
$q = $conn->query("SELECT uname FROM Student WHERE uname= $aRegUsr ");
$stmt = $conn->prepare($q);
$rows = $stmt->fetchAll();
if ($rows > 1) { # The username already exists
#... rest of your code
精彩评论