MySqli中出现意外错误

时间:2015-12-31 18:04:17

标签: php mysqli

有人可以向我解释一下,出了什么问题?为什么所有这些错误?

  

注意:未定义的变量:用户名在   第5行的C:\ xampp \ htdocs \ Overall \ Arbete \ CMS \ core \ functions \ users.php

     

警告:mysqli_real_escape_string()只需要2个参数,1   在C:\ xampp \ htdocs \ Overall \ Arbete \ CMS \ core \ functions \ general.php中给出   第17行

     

注意:未定义的变量:con in   第6行的C:\ xampp \ htdocs \ Overall \ Arbete \ CMS \ core \ functions \ users.php

     

警告:mysql_query()期望参数2是资源,字符串   在C:\ xampp \ htdocs \ Overall \ Arbete \ CMS \ core \ functions \ users.php中给出   在第6行

     

警告:mysql_result()期望参数1为resource,null给定   在C:\ xampp \ htdocs \ Overall \ Arbete \ CMS \ core \ functions \ users.php上线   6

的index.php:



<?php
    require 'core/init.php';
?>
<!doctype HTML>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="shortcut icon" href="img/favicon.ico"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="css/stylesheet.css"/>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <title>Administraton</title>
    </head>

<body>

<?php
logged_in_redirect();
if (empty($_POST) === false) {
    $username =    $_POST['username'];
    $password = $_POST['password'];

    if (empty($username) === true || empty($password) === true) {
        $errors[] = '<style>input[type=text], input[type=password] { border: 1px solid red; }</style>';
    } else if (user_exists($username) === false) {
        $errors[] = '';
    } else if (user_active($username) === false) {
        $errors[] = '';
    } else {

        if (strlen($password) > 32) {
            $errors[] = '';
        }
        $login = login($username, $password);
        if ($login === false) {
            $errors[] = '';
        } else {
            $_SESSION['user_id'] = $login;
            header('Location: ./');
            exit();
        }
    }
} else {
    $errors[] = '';
}
if (empty($errors) === false) {
    echo output_errors($errors);
}
?>
<div id="login">
    <p style="position:relative;text-align:center;top:25px;">
        <img src="img/auth.png" alt="Sign in" style="width:50px;height:50px;background-color:#222;border-radius:50%;padding:10px;">
    </p>
    <li style="margin:15px;list-style-type:none;margin-top:75px;">
        <form method="POST" name="login">
            <div class="form-group">
                <input type="text" name="username" class="form-control" placeholder="Användarnamn">
            </div>
            <div class="form-group">
                <input type="password" name="password" class="form-control" placeholder="Lösenord">
            </div>
            <button type="submit" name="submit" class="btn btn-default" style="width:100%;background-color:#35b3EE;border:none;color:white;" id="myButton" data-loading-text="Loading...">Logga in</button>
        </form>
    </li>
</div>

</body>
</html>
&#13;
&#13;
&#13;

的init.php:

&#13;
&#13;
<?php
$con = mysqli_connect("localhost","root","","cms");

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

error_reporting(E_ALL);
require 'functions/general.php';
require 'functions/users.php';
?>
&#13;
&#13;
&#13;

database.php中:

&#13;
&#13;
<?php
$con = mysqli_connect("localhost","root","","cms");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>
&#13;
&#13;
&#13;

Users.php:

&#13;
&#13;
<?php
// User exists

function user_exists() {
    $username = sanitize($username);
    return (mysql_result(mysql_query($con, "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"), 0) == 1) ? true : false;
}

// Login
function login($username, $password) {
    $user_id = user_id_from_username($username);

    $username = sanitize($username);
    $username = mysql_real_escape_string($con, sanitize($username));
    $password = md5($password);

    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"), 0) == 1) ? $user_id : false;
}

// Logged in
function logged_in() {
    return (isset($_SESSION['user_id'])) ? true : false;
}

?>
&#13;
&#13;
&#13;

General.php:

&#13;
&#13;
<?php
// Logged in Session Redirect
function logged_in_redirect() {
    if (logged_in() === true) {
        header('Location: index');
        exit();
    }
}

// Array Data
function array_sanitize(&$item) {
    $item = htmlentities(strip_tags(mysqli_real_escape_string($item)));
}

// Sanitize Data
function sanitize($data) {
    return htmlentities(strip_tags(mysqli_real_escape_string($data)));
}

// Output errors
function output_errors($errors) {
    return '<ul><li><br>' . implode('</li><li><br>', $errors) . '</li></ul>';
}
?>
&#13;
&#13;
&#13;

&#13;
&#13;
Notice: Undefined variable: username in C:\xampp\htdocs\Overall\Arbete\CMS\core\functions\users.php on line 5

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\Overall\Arbete\CMS\core\functions\general.php on line 17

Notice: Undefined variable: con in C:\xampp\htdocs\Overall\Arbete\CMS\core\functions\users.php on line 6

Warning: mysql_query() expects parameter 2 to be resource, string given in C:\xampp\htdocs\Overall\Arbete\CMS\core\functions\users.php on line 6

Warning: mysql_result() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Overall\Arbete\CMS\core\functions\users.php on line 6
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

Users.php下面的函数

function user_exists() {
    $username = sanitize($username);
    return (mysql_result(mysql_query($con, "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"), 0) == 1) ? true : false;
}

应阅读:

function user_exists($username) {
    $username = sanitize($username);
    return (mysql_result(mysql_query($con, "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"), 0) == 1) ? true : false;
}

&函数中有一个array_sanitize。你可能想删除它。

看看是否能带你去你想去的地方。