I have this code:
class user
{
public $name ;
public $password ;
//constructor
function __construct($username, $password)
{
$dbhost = 'localhost';
$dbuser = 'root' ;
$dbpass = '' ;
$db = 'privelidge';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
$sql= "select * from `user` where Name = '".$username."' and password = '".$password."'" ;
$result = mysql_query($sql);
$found = false;
while( $row = mysql_fetch_array($result)){
$this->name = $username ;
$this->password = $password ;
$found = true;
}
}
//method add user
public static function add_user( $username , $password )
{
$dbhost = 'localhost';
$dbuser = 'root' ;
$dbpass = '' ;
$db = 'privelidge';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
$u = new User($username, $password);
$u->name = $username ;
$u->password = $password ;
$u = User::search($username);
if($u == null )
{
$sql = "INSERT INTO `user` VALUES ('".$username."', '".$password."')";
mysql_query($sql);
echo " user has been added ";
exit;
}
else
{
echo " username already existed ";
exit;
}
}
public static function search($username)
{
$dbhost = 'localhost';
$dbuser = 'root' ;
$dbpass = '' ;
$db = 'privelidge';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
$sql = " SELECT * FROM `user` WHERE Name = '".$username."'" ;
$results = mysql_query($sql);
while($row = mysql_fetch_row($results)){
$name = $row['Name'];
$password = $row['password'];
$user = new user($name, $password);
return $user;
}
return null;
}
I got this message in the browser :
Notice: Undefined index: Name in C:\xampp\htdocs\proj\Document1.php on line 105
Notice: Undefined index: password in C:\xampp\htdocs\proj\Document1.php on line 106
those two lines refers to the folowing peice of code:
$name = $row['Name'];
$password = $row['password'];
even though I have already had Name
and password
in my database.
I am really confused would you help me please.