|
|
|
ติดปัญหาในการเขียน login แบบ oop รบกวนพี่ๆช่วยแนะนำที่ครับ |
|
|
|
|
|
|
|
Code (PHP)
<?php
class Database {
private $conn;
public function __construct($servername = "localhost", $dbname = "customer", $username = "sa", $password = "123456"){
try {
$this->conn = new PDO("sqlsrv:server=$servername ; Database = $dbname", $username, $password);
$this->conn->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
//echo "Connect DB Success";
} catch (PDOException $e) {
echo "Connect DB Failed".$e->getMessage;
}
}
public function getRow($query, $params = []){
$stmt->$this->conn->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
}
public function login($username,$password){
$stmt->$this->getRow("SELECT * FROM tb_user WHERE username=? AND password=?",["$username","$password"]);
if ($stmt == true) {
echo "login success";
}else{
echo "login failed";
}
}
}
Code (PHP)
<?php
include 'db.php';
$db = new Database();
if (isset($_POST['submit']) && $_POST['username'] && $_POST['password']) {
$username = $_POST['username'];
$password = $_POST['password'];
$login = $db->login($username,$password);
echo $login;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="post" action="index.php">
<label>username</label>
<input type="text" name="username" id="username"></input><br><br>
<label>password</label>
<input type="password" name="password" id="password"></input><br><br>
<input type="submit" name="submit" value="login"></input>
</form>
</body>
</html>
Tag : PHP, Ms SQL Server 2012
|
ประวัติการแก้ไข 2016-06-04 00:01:31
|
|
|
|
|
Date :
2016-06-03 23:59:44 |
By :
indygunaza |
View :
876 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่ได้ลองนะ
Code (PHP)
<?php
class Database {
private $conn;
public function __construct($servername = "localhost", $dbname = "customer", $username = "sa", $password = "123456"){
try {
$this->conn = new PDO("sqlsrv:server=$servername ; Database = $dbname", $username, $password);
$this->conn->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
//echo "Connect DB Success";
} catch (PDOException $e) {
echo "Connect DB Failed".$e->getMessage;
}
}
public function getRow($query, $params = []){
$stmt = $this->conn->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
}
public function login($username,$password){
$rs = $this->getRow("SELECT * FROM tb_user WHERE username=? AND password=?",["$username","$password"]);
if ($rs == true) {
echo "login success";
}else{
echo "login failed";
}
}
}
|
ประวัติการแก้ไข 2016-06-04 08:09:54
|
|
|
|
Date :
2016-06-04 08:06:27 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าจะให้ Security
function getRow เปลี่ยนเป็น Private
|
|
|
|
|
Date :
2016-06-04 08:07:53 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getRow -> return มาเป็น array
แต่ function login กลับมาตรวจสอบ true
มันไม่สอดคล้องกันน่ะ
อาจจะ $stmt->rowCount();
|
ประวัติการแก้ไข 2016-06-04 08:15:54
|
|
|
|
Date :
2016-06-04 08:11:02 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้แล้วครับ ขอบคณครับ
|
|
|
|
|
Date :
2016-06-04 10:36:31 |
By :
indygunaza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2016-06-06 10:47:57 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|