[User Login and Registration] Can't check if username exists

Post here is you are having problems with any of the tutorials.
Post Reply
dmillerw
Posts: 4
Joined: Tue Jun 21, 2011 11:26 am

[User Login and Registration] Can't check if username exists

Post by dmillerw »

So I've been following your tutorial, and everything seems to work, errors and verifying, except for the preventing of more then one of the same username.
<?php

error_reporting(1);

// checks if username is taken
function user_exists($user){
	$user = mysql_real_escape_string($user);
	
	$total = mysql_query("SELECT COUNT('id') FROM 'users' WHERE 'username' = '{$user}'");

	return (mysql_result($total, 0) == '1') ? true : false;
}

// checks if username & pass are valid
function valid_credientials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = sha1($pass);

	$total = mysql_query("SELECT COUNT('id') FROM 'users' WHERE 'username' = '{$user}' AND 'password' = '{$pass}'");

	return (mysql_result($total, 0) == '1') ? true : false;
}

// adds new user & pass to database
function add_user($user, $pass, $realname, $email){
	$user = mysql_real_escape_string(htmlentities($user));
	$pass = sha1($pass);
	$realname = mysql_real_escape_string(htmlentities($realname));
	$email = mysql_real_escape_string(htmlentities($email));

	mysql_query("INSERT INTO users (username, password, realname, email) VALUES ('$user', '$pass', '$realname', '$email')") or die(mysql_error());
}

?>
This always gives me an error, when asked to check for the username.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'username' = 'dmillerw'' at line 1
SELECT COUNT('id') FROM 'users' WHERE 'username' = '{$user}'
What am I missing?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: [User Login and Registration] Can't check if username ex

Post by jacek »

error_reporting(1);
should be
error_reporting(E_ALL);
this may highlight the problem.

But it looks like you used the wrong type of quotes, table and column names should be quoted with backticks ` not quotes '
Image
dmillerw
Posts: 4
Joined: Tue Jun 21, 2011 11:26 am

Re: [User Login and Registration] Can't check if username ex

Post by dmillerw »

This is what happens when I do this at three in the morning. Thanks!

How do you actually "write" backticks though? Running Ubuntu.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: [User Login and Registration] Can't check if username ex

Post by jacek »

dmillerw wrote:This is what happens when I do this at three in the morning. Thanks!
That's the best time for it :)
dmillerw wrote:How do you actually "write" backticks though? Running Ubuntu.
They should be on your keyboard, to the left of he numbers at the top, the key that has three things on it.
Image
dmillerw
Posts: 4
Joined: Tue Jun 21, 2011 11:26 am

Re: [User Login and Registration] Can't check if username ex

Post by dmillerw »

*facepalm* ...thanks. xD
Post Reply