Page 1 of 1

Login Probs again D:

Posted: Tue Dec 11, 2012 2:17 am
by Z645
Hai guys. So my problem is that My user.inc.php file is all correct but it shows the error
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/a4495253/public_html/core/inc/user.inc.php on line 19
My user.inc.php file:
<?php

// Checks if the given username exists in the table.
function user_exists($user){
	$user = mysql_real_escape_string($user);
	
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'");
	
	return (mysql_result($total, 0) == '1') ? true : false;
}

// Checks if the given username and password combination is valid.
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string(htmlentities($user));
	$pass = sha1($pass);
	
	mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}' AND `user_password` = '{$password}'");
	
	return (mysql_result($total, 0) == '1') ? true : false;
}

// Adds a user to the Database.
function add_user($user, $pass, $first_name, $last_name){
	$user = mysql_real_escape_string(htmlentities($user));
	$pass = sha1($pass);
	$first_name = mysql_real_escape_string(htmlentities($first_name));
	$last_name = mysql_real_escape_string(htmlentities($last_name));
	
	mysql_query("INSERT INTO `users` (`user_name`, `user_password`, `first_name`, `last_name`) VALUES ('{$user}', '{$pass}', '{$first_name}', '{$last_name}')");
}
?>
login.php:
<?php

include('core/init.inc.php');

$errors = array();

if (isset($_POST['username'], $_POST['password'])){
	if (empty($_POST['username'])){
		$errors[] = 'Please enter your Username!';
	}
	
	if (empty($_POST['password'])){
		$errors[] = 'Please enter your Password!';
	}
	
	if (valid_credentials($_POST['username'], $_POST['password']) === false){
		$errors[] = 'Username or Password is incorrect!';
	}
	
	if (empty($errors)){
		$_SESSION['username'] = htmlentities($_POST['username']);
		
		header('Location: home.php');
		die();
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Login to your Profile!</title>
		<meta name="description" content="Welcome to Socialcrunch. You may connect with friends, meet new ones, and also hang out when you have free time!" />
		<meta name="keywords" content="social, networking, games, chat, hangout, friends" />
		<meta name="robots" content="index, follow" />

		<link rel="stylesheet" href="styles/form.css" type="text/css">
		<script type="text/javascript">
  WebFontConfig = {
    google: { families: [ 'Grand+Hotel::latin' ] }
  };
  (function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
      '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
  })(); </script>
	</head>
	
	<body>
	
		<div id="wrapper">
			<div class="login">
				<div>
				<?php
				
				if (empty($errors) === false){
					?>
					<ul>
						<?php
						
						foreach ($errors as $error){
							echo "<li>{$error}</li>";
						}
						
						?>
					</ul>
					<?php
				}
				
				?>
				</div>
				<center>
				<b>Login to an Existing Profile.</b>
				<form action="" method="post">
					<p class="username">
						Username: <input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" /><br />
					</p>
					<p class="password">
						Password: <input type="password" name="password" id="password" /><br />
					</p>
					<p class="submit">
						<input type="submit" name="submit" value="Login"><br />
					</p>
				</form>
					Don't have a Profile yet? <a href="signup.php">Sign up!</a><br />
					Forgot your Password? <a href="">Reset Password</a>
				</center>
			</div>
		</div>
	
	</body>
</html>

Re: Login Probs again D:

Posted: Tue Dec 11, 2012 2:27 am
by ExtremeGaming
	
	mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}' AND `user_password` = '{$password}'");
	
	return (mysql_result($total, 0) == '1') ? true : false;
You aren't giving your query the variable name of $total so mysql_result is throwing an error because there's no query to act upon

Re: Login Probs again D:

Posted: Fri Dec 14, 2012 12:44 am
by jacek
Just to clarify the above, you need to assign the result of the query to a variable so you can use it lower down.
$total = mysql_query( ...