im trying to build a login system i create some functions one of it is to return the user_id but it dosn't work with mysql_result() here is my code
My functions
//security
function sanitize($input){
$input= mysql_real_escape_string($input);
$input=trim($input," ");
return $input;
}
//get user_id
function get_user_id($username){
$query=mysql_query("SELECT 'user_id' FROM user_admin WHERE username = '$username'");
return mysql_result($query, 0, "user_id");
}
//check username and pass
function login($username , $password){
$username=sanitize($username);
$password=md5($password);
$user_id=get_user_id($username);
$query=mysql_query("SELECT COUNT('user_id') FROM user_admin WHERE username='$username' AND password='$password' ");
return (mysql_result($query , 0) == 1) ? $user_id : false ;
}
my login page code :
<?php
require_once('../requ/conn.inc.php');
require_once('functions/inc.f.php');
//check post existance
if(empty($_POST) === false){
//check login fields not empty if so error
if(empty($_POST['username']) || empty($_POST['password'])){
$errors[]="All fields should be field in";
}else{
//security # mysql injaction
$username=sanitize($_POST['username']);
$password=$_POST['password'];
//check username existance in DB
$query=mysql_query("SELECT COUNT('user_id') FROM user_admin WHERE username='$username' ");
$username_existance=mysql_result($query,0);
if($username_existance != 1){
$errors[]="Invalid Username please try again";
}else{
//check username and password
$login=login($username , $password);
if($login=== false){
$errors[]=" Invalid Username And/Or Password try again. ";
}else {
die($login);
}
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Administration Raffle -Login-</title>
<link rel="stylesheet" type="text/css" href="style/general.css"/>
</head>
<body>
<div id="container">
<div id="header">
<h1>Administration -Back Office-</h1>
<h4>The big Raffle of the year</h4>
</div>
<div id="body">
<div id="menu-h">
<ul>
<li><a href="index.php">Home</a></li><a href="prices.php">Prices</a><a href="addprice.php">Add price</a><a href="participants.php">Participants</a><a href="winners.php">Winners</a>
</ul>
<ul id="login"><li><a href="login.php">Login</a></li></ul>
</div>
<div id="content">
<?php
if(isset($errors)) {output_errors($errors); }
//echo $username." ".$username_existance;
?>
<form method="post" action="login.php" name="login">
<p> <label for="username"> Username : </label> <input type="text" id="username" name="username" value=" " /> </p>
<p><label for="password"> Username : </label> <input type="password" id="password" name="password" value="" /></p>
<p> <input type="submit" name="login" value="Login"/> </p>
</form>
</div>
</div>
<div id="footer">
<p class="cr">CHANNARK All Rights reserved © 2013</p>
</div>
</div>
</body>
</html>
this code should displays the user_id witch is an integer but what it display now is that: user_id
Thanks