mysqli连接并从数据库错误中选择

时间:2014-03-09 19:49:11

标签: php mysql database mysqli database-connection

我最近开始使用mysqli_连接到我的数据库,因为sql注入和缺乏安全性的问题。我正在尝试使用mysql从mysql重新配置我的登录页面,但页面不会加载,我不知道为什么。提前感谢您的帮助。

这是我的代码:

<?php
$mysqli = new mysqli("localhost", "username", "password", "db");

if($mysqli->connect_errno > 0){
    die('Unable to connect to database [' . $mysqli->connect_error . ']');
}

if (!isset($_SESSION['email'])) {

$e = trim($_REQUEST['email']);
$email = mysqli->real_escape_string($e);
$p = trim($_REQUEST['password']);
$password = mysqli->real_escape_string($p);

if ($result = $mysqli->query("SELECT email, password" .
                                             " FROM users" .
                                             " WHERE email = '".$email."' AND password = '".$password."'")) {
    printf("Select returned %d rows.\n", $result->num_rows);

if ($result->num_rows == 1) {
$row = $result->fetch_array(MYSQLI_NUM);
   $user_id = $ow['user_id'];
   //No more setcookie
   $_SESSION['user_id'] = $user_id;
   $_SESSION['email'] = $email;
}

    /* free result set */
    $result->close();
}

}
?>
<html>
<head>

<title>Login</title>

</head>
<body>

<form id="signin_form"
               action="<?php echo $_SERVER['PHP_SELF']; ?>"
               method="POST">
      <div class="signin_box">
         <label for="email">Email or Username:</label><br>
         <input type="text" name="email" id="email" size="30" />
         <br />
         <label for="password">Password:</label>
         <input type="password" name="password" id="password" size="30" />

     <br />
<span class="signin_submit">
        <input type="submit" value="Sign In" class="signin_submit" />
</span>
     </div>
   </form>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

下面

$email = mysqli->real_escape_string($e);

你忘记了$

$email = $mysqli->real_escape_string($e);

$password = mysqli->real_escape_string($p);

$password = $mysqli->real_escape_string($p);