I'm trying to make a login/register system and when you press login it should say exists...
<?php
include 'core/init.php';
function user_exists($username){
$username = sanitize($uusername);
$query = mysql_query("SELECT COUNT (`user_id`) FROM `users` WHERE `username` = '$username'");
$result = mysql_query($query) or die(mysql_error()); // Check if query was successful
$row = mysql_fetch_array($result); // fetch the first row
return ($row[0] > 1); // If there is one or more users with this name, return true.
}
Here is the code that should make the page say "Exists":
<?php
include 'core/init.php';
if(user_exists('Zuzima') === true) {
echo 'exists';
}
die();
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username) === true || empty($password) === true){
$errors[] = 'You need to a username and password.';
} else if (user_exists($username) === false) {
$errors[] = 'We can\'t find that username. Have you registered?';
}
}
?>
Please help me I've been working on this for 6 months at the least.
If there is one or more users with this name, return true.
So shouldn't return ($row[0] > 1);
just be return ($row[0] >= 1);
You must alias any aggregates to use in an array.
SELECT COUNT (`user_id`) AS userIdCount ...
It would just be easier to return a single column and use the row count to determine if there is a record for your user.
Further, mysql_
functions have been deprecated. You need to use PDO
or mysqli_
with prepared statements.
The $query
is getting assigned a resource handle from mysql_query
, then you are passing that resource handle in as a string to mysql_query
$query = mysql_query("SELECT COUNT (`user_id`) FROM `users` WHERE `username` = '$username'");
$result = mysql_query($query) or die(mysql_error());
should be
$result = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'") or die(mysql_error())
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.