Thanks. I would like to let user login , And i try to do it in oop way
Here is the loginPage.php
<html>
<body>
<?php require_once("connection.php");
$instance=new connection("527442_1","chi1234a","foodil_zxq_1");
if (isset($_POST['s'])) {
$SecIns=new login($_POST['u'],$_POST['p']);
echo $SecIns->enter();
}
?>
<form name="fm" method="POST" action="show.php">
User Name :<input type="text" name="u">
<br>
Password: <input type="password" name="p">
<br>
<input type="submit" name="s">
</form>
</body>
</html>
Here is the oop code which i fail Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource
<?php
class connection
{var $user;
var $pass;
var $db;
function __construct($user,$pass,$db){
$this->user=$user;
$this->pass=$pass;
$this->db=$db;
}
function conn(){
$conn=mysql_connect('localhost' , $this->user , $this->pass) or die ("Can't connect server");
mysql_select_db($this->db,$conn) or die ("DB error");
}
}
class login extends connection
{
function __construct($id,$pwd)
{$this->id=$id;
$this->pwd=$pwd;
}
function enter()
{$this->query="SELECT * FROM test WHERE id='".$this->id."' AND pwd='". $this->pwd."';";
$result=mysql_query($this->query,$this->conn) or die ("Error:" . mysql_error());
if (mysql_num_rows($this->result)<1)
return "Not correct user";
else
return "Login success";
}
}
?>
The problem is at the function enter() .Should be the format problem?? But i spend a long time on it . Thanks.
While it’s commendable that you want to add organization to your code, adding the methods into a class doesn’t automatically make it OOP. I recommend you read up on the basics and then reorganize your classes.
As for your problem,
Those variables you passed are treated as literal strings because you’ve quoted them. you need to pass it an actual query and a connection object. Just remove the quotes.
Also, I recommend you look into parametrized queries : http://php.net/manual/en/book.pdo.php
EDIT
Looking at your code further, these tips might improve your code
Instantiate your connection and pass it as a parameter for your other classes. Don’t extend them from the connection.
$conn = new Connection($username, $pass, $host, $db);
$login = new Login($conn);
$login->doLogin($username, $password);
And have a
query()method in your connection, so you can call that method from your other classes.