我正在重写我的登录脚本。这个练习的重点是让我的连接处理程序关闭页面,因为我被告知了这一点。我在说的是这个。
var= mysqli_connect(xxxx)
因此,我们尝试将其转移到一个函数中。
function openDB() {
include("/assets/configs/db_config.php");
$conn = mysqli_connect('x', 'x', 'x', 'x');
//$conn = mysqli_connect($config["host"] , $config["username"], $config["password"],$config["dbname"]);
// 1. Create a database connection
//$conn = mysqli_connect("x" , "x", "","x");
if (!$conn)
{
$this->error_msg = "connection error could not connect to the database:! ";
return false;
}
$this->conn = $conn;
return true;
}
但是,据我所知,我已经在另一个问题中提到了这一点,我不会要求任何人特别回顾这个功能。基本上无论我尝试什么,我都被卡住了,因为 mysqli_real_escape_string 协议出现错误(对象而不是连接)
继续前进,我还有一些包含mysqli_real_escape_string
用法的其他功能,因此我想调整一个以用于登录功能。这是一个有效的方法。这是一个更新功能。
function member_update($mid, $name, $address, $postcode, $photo)
{
$esc_mid = mysqli_real_escape_string($this->conn, $mid);
$esc_name = mysqli_real_escape_string($this->conn, $name);
$esc_address = mysqli_real_escape_string($this->conn, $address);
$esc_postcode = mysqli_real_escape_string($this->conn, $postcode);
$esc_photo = mysqli_real_escape_string($this->conn, $photo);
$sql = "UPDATE member SET
mid='{$esc_mid}',
name='{$esc_name}',
address='{$esc_address}',
postcode='{$esc_postcode}',
photo='{$esc_photo}'
WHERE mid='{$esc_mid}'";
$result = mysqli_query($this->conn, $sql);
if ($result) {
$numofrows = mysqli_affected_rows($this->conn);
echo("$esc_mid, {$esc_name}, $esc_address, {$esc_postcode}, $esc_photo"); /*see id*/
$numofrows = mysqli_affected_rows($this->conn);
return $numofrows;
}
else
$this->error_msg = "could not connect for some wierd reason";
return false ;
}
现在,它站着,这就是我被困的地方。我试图将上述功能修改为可用于处理登录的功能。这里是。
function logcon($user, $password )
{
$esc_user = mysqli_real_escape_string($this->conn, $user);
$esc_password = mysqli_real_escape_string($this->conn,$password);
$sql = "select * all from users where username ='{$user}' AND password='{$password}'";
$result = mysqli_query($this->conn, $sql);
if ($result) {
$numofrows = mysqli_affected_rows($this->conn);
return $numofrows;
}
else
$this->error_msg = "could not connect for some wierd reason";
return false ;
}
据我所知,逻辑足够接近,所以它应该可以工作。但事实并非如此。mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
在这段代码中, 我被页面侧刺痛了。$row=mysqli_fetch_array($result, $sqL);
这是我的登录脚本(减去 CSS 和表单)
if(isset($_POST['submit'])){
$user=$_POST['user'];
$password=$_POST['password'];
//To ensure that none of the fields are blank when submitting the form if
if(isset($_POST['user']) && isset($_POST['password']))
{
$user = stripslashes($user);
$password = stripslashes($password);
$db1=new dbmember();
$db1->openDB();
$sql="SELECT * FROM users WHERE username='{$user}' AND password='{$password}'";
$result=$db1->logcon($user, $password);
$row=mysqli_fetch_array($result, $sqL);
if($row[0]==1)
{
session_start();
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
$_SESSION['loggedin'] = "true";
header("location:index.php");
}
else
{
print ('<div id="error">Acess denied, wrong username or password?</div>');
}
}
else
{
print ('<div id="error">Enter something!</div>');
}
}
?>