mysql和mysqli的问题

时间:2018-05-17 10:29:23

标签: php html css mysql mysqli

  

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

     

注意:未定义的变量:conn in   第85行的C:\ xampp \ htdocs \ swift \ core \ functions \ users.php

     

警告:mysqli_query()期望参数1为mysqli,null给定   第85行的C:\ xampp \ htdocs \ swift \ core \ functions \ users.php

     

致命错误:未捕获错误:调用未定义的函数   mysqli_result()在C:\ xampp \ htdocs \ swift \ core \ functions \ users.php:86   堆栈跟踪:#0 C:\ xampp \ htdocs \ swift \ loginact.php(14):f_exists('')   在第86行的C:\ xampp \ htdocs \ swift \ core \ functions \ users.php中抛出#1 {main}

我继续得到这些错误,但我找不到办法,我已经尝试了一切..希望有人可以帮助我,因为我是新手。 我一直这样做已经差不多一天了,我无法找到答案。

这是General.php。

<?php

     $connect_error = 'Sorry, there was some connectivity issue!';
    $conn = mysqli_connect('localhost','root','');
    $db = mysqli_select_db($conn, 'swift');


    function activation($to, $subject, $body) {
        mail($to, $subject, $body, 'From: swift@srikanthnatarajan.com');
    }

    function recovery_user_pass($to, $subject, $body) {
        mail($to, $subject, $body, 'From: swift@srikanthnatarajan.com');
    }

    function f_protect_page() {
        if(f_logged_in() === false) {
            header('Location: flogin.php');
            exit();     
        } 
    }

    function user_protect_page() {
        if(f_logged_in() === false) {
            header('Location: fprotect.php');
            exit();     
        } 
    }

    function use_protect_page() {
        if(f_logged_in() === true) {
            header('Location: fprotect.php');
            exit();     
        } 
    }

    function f_logged_in_redirect() {
        if(f_logged_in() === true) {
            header('Location: index.php');
            exit();
        }
    }

    function array_sanitize($item) {
        $item = htmlentities(strip_tags(mysqli_real_escape_string($item)));
    }

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

    function output_errors($errors) {
        return '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
    }

?>

这是USERS.PHP可以有人请检查一下。

<?php

$connect_error = 'Sorry, there was some connectivity issue!';
    $conn = mysqli_connect('localhost','root','');
    $db = mysqli_select_db($conn, 'swift');



    function f_recover($mode, $f_mailid) {
        $mode = sanitize($mode);
        $f_mailid = sanitize($f_mailid);
        $f_data = f_data(f_id_from_email($f_mailid),'f_id','f_fname','f_uname');
        if ($mode == 'f_uname') {
            recovery_user_pass($f_mailid, 'Recovery: Your username', "Hello " . $f_data['f_fname'] . ",\n\nYour username is: " . $f_data['f_uname'] . "\n\n-Swift Airlines");
        }
        else if($mode == 'f_password') {
            $generated_password = substr(md5(rand(999, 999999)), 0, 8);
            f_change_password($f_data['f_id'], $generated_password);

            update_f($f_data['f_id'], array('f_passrec' => '1'));

            recovery_user_pass($f_mailid, 'Recovery: Your password', "Hello " . $f_data['f_fname'] . ",\n\nYour new password is: " . $generated_password . "\n\n-TOFSIS");
        }
    }

    function f_activate($f_mailid, $f_mailcode) {
        $f_mailid = mysqli_real_escape_string($_POST['f_mailid']);
        $f_mailcode = mysqli_real_escape_string($_POST['f_mailcode']);
        if(mysqli_result(mysqli_query("SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_mailid` = '$f_mailid' AND `f_mailcode` = '$f_mailcode' AND `f_active` = 0"), 0) == 1) {
            mysqli_query("UPDATE `flight_users` SET `f_active` = 1 WHERE `f_mailid` = '$f_mailid' ");
            return true;
        }
        else {
            return false;
        }
    }

    function update_f($f_id, $update_data) {
        $update = array();
        array_walk($update_data, 'array_sanitize');
        foreach ($update_data as $field => $data) {
            $update[] = '`' . $field . '` = \'' . $data . '\'';
        }       
        mysqli_query("UPDATE `flight_users` SET " . implode(', ',$update) . "WHERE `f_id` = $f_id") or die(mysqli_error($conn));
    }

    function f_change_password($f_id, $f_password) {
        $f_id = (int)$f_id;
        $f_password = md5($f_password);
        mysqli_query("UPDATE `flight_users` SET `f_password` = '$f_password', `f_passrec` = 0 WHERE `f_id` = $f_id");
    }

    function register_f($register_data) {
        array_walk($register_data, 'array_sanitize');
        $register_data['f_fname'] = ucwords(strtolower($register_data['f_fname']));
        $register_data['f_lname'] = ucwords(strtolower($register_data['f_lname']));
        $register_data['f_password'] = md5($register_data['f_password']);
        $register_data['f_uname'] = strtolower($register_data['f_uname']);
        $fields = '`' . implode('`, `', array_keys($register_data)) . '`';
        $data = '\'' . implode('\', \'', $register_data) . '\'';

        mysqli_query("INSERT INTO `flight_users` ($fields, `f_regdate`) VALUES ($data, NOW())");
        activation($register_data['f_mailid'], 'Swift Airlines: Activate your account', "Hello " . $register_data['f_fname'] . ", \n\nYou need to activate your account in order to use the features of Swift Airlines. Please click the link below: \n\nhttp://srikanthnatarajan.com/swift/activate.php?f_mailid=" . $register_data['f_mailid'] . "&f_mailcode=" . $register_data['f_mailcode'] . " \n\n-Swift Airlines");
    }

    function f_data($f_id){
        $data = array();
        $f_id = (int)$f_id;
        $func_num_args = func_num_args();
        $func_get_args = func_get_args();
        if($func_num_args > 1) {
            unset($func_get_args[0]);
            $fields = '`'. implode('`, `', $func_get_args) . '`'; 
            $data = mysqli_fetch_assoc(mysqli_query("SELECT $fields FROM `flight_users` WHERE `f_id` = $f_id"));            
            return $data;
        }
    }

    function f_logged_in() {
        return (isset($_SESSION['f_id'])) ? true : false;
    }

    function f_exists($f_uname) {
        $f_uname = sanitize($f_uname);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname'");
        return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
    }

    function f_email_exists($f_mailid) {
        $f_mailid = sanitize($f_mailid);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_mailid`= '$f_mailid'");
        return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
    }

    function f_regid_exists($f_regid) {
        $f_regid = sanitize($f_regid);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_regid`= '$f_regid'");
        return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
    }

    function f_active($f_uname) {
        $f_uname = sanitize($f_uname);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname' AND `f_active` = 1");
        return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
    }

    function f_id_from_username($f_uname) {
        $f_uname = sanitize($f_uname);
        $query = mysqli_query($conn, "SELECT `f_id` FROM `flight_users` WHERE `f_uname` = '$f_uname'");
        return mysqli_result($conn, $query, 0, 'f_id');
    } 

    function f_id_from_email($f_mailid) {
        $f_mailid = sanitize($f_mailid);
        $query = mysqli_query($conn, "SELECT `f_id` FROM `flight_users` WHERE `f_mailid` = '$f_mailid'");
        return mysqli_result($conn, $query, 0, 'f_id');
    } 

    function f_login($f_uname, $f_password) {
        $f_id = f_id_from_username($f_uname);
        $f_uname = sanitize($f_uname);
        $f_password = md5($f_password);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname' AND `f_password` = '$f_password'");
        return (mysqli_result($conn, $query, 0) == 1) ? $f_id : false;
    }

?>

,这是Loginact.php ------------------

<?php

        $title = 'Swift Airlines | Login Error';
    include $_SERVER["DOCUMENT_ROOT"].'/swift/core/init.php';


    if(empty($_POST) === false) {
        $f_uname = $_POST['f_uname'];
        $f_password = $_POST['f_password'];

        if(empty($f_uname) === true || empty($f_password) === true){
            $errors[] = 'You need to enter both, the username and the password!';
        } 
        else  if (f_exists($f_uname)===false) {
            $errors[] = 'No such user exists! Please register!';
        }
        else if(f_active($f_uname)===false) {
            $errors[] = 'Please activate your account!';
        }
        else {

            if(strlen($f_password)>32) {
                $errors[] = 'Password too long!';
            }

            $f_login = f_login($f_uname, $f_password);
            if($f_login===false) {
                $errors[] = 'Username and Password do not match!';
            }
            else {
                $_SESSION['f_id'] = $f_login;
                header('Location: http://localhost/swift/index.php');
                exit();
            }
        }       
    }
    else {
            $errors[] = 'No Log In credentials received!';
        }

    include $_SERVER["DOCUMENT_ROOT"].'/swift/includes/overall/header.php';

    if(empty($errors) === false) {
?>
    <br/><h4>We tried to log you in, but : </h4><br/>

<?php
    echo output_errors($errors);
    }

    include $_SERVER["DOCUMENT_ROOT"].'/swift/includes/overall/footer.php';

?>

我继续得到这些错误,但我找不到办法,我已经尝试了一切..希望有人可以帮助我,因为我是新手。 我一直这样做已经差不多一天了,我无法找到答案。

我知道php有点难,我一直在寻找答案近一天了。我只是编程的新手,我尽我所能努力学习这些东西,希望有人可以帮助我。

我希望我能在这里学到更多关于php和其他编程语言的知识..即使我现在正在问我也要继续寻找答案。但是非常感谢。

3 个答案:

答案 0 :(得分:1)

我怀疑你尝试了一切!

看起来好像是在尝试从mysql_ functions迁移到mysqli _。

来自manual的mysqli_fetch_array:

$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = mysqli_query($link, $query);

/* numeric array */
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);

$ link是你的mysqli连接。在你的代码中它是$ conn。

在您的功能中,您有范围问题。 $ conn在全局范围内,因此未在函数范围内设置。

将其分解,从您的f_id_from_email函数开始,然后按照并尝试翻译手册的示例。

如果阅读,错误消息实际上非常有用。但是,当你面对许多人时,你可能会不堪重负。

  

调用未定义的函数mysqli_result()

那是因为没有mysqli_result函数。

答案 1 :(得分:0)

1- general.php第49行:查看mysqli_real_escape_string,在程序代码中,您必须指定2个参数,链接(或连接)和字符串。

function array_sanitize($conn,$item) {
        $item = htmlentities(strip_tags(mysqli_real_escape_string($conn,$item)));
    }

function sanitize($conn,$data) {
    return htmlentities(strip_tags(mysqli_real_escape_string($conn,$data)));
}

2- users.php,在第85行,$ conn在函数范围内不存在。将$ conn作为参数传递给函数并使用($ conn,$ f_uname)调用它。

function f_exists($conn,$f_uname) {
    $f_uname = sanitize($f_uname);
    $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname'");
    return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
}

3-第86行的问题,与我的#2点相同。

答案 2 :(得分:-3)

  

注意:未定义的变量:conn ...   添加函数 global $ conn 。这是一个非常糟糕的做法

function f_exists($f_uname, $conn) {
    // or global $conn;
    $f_uname = sanitize($f_uname);
    $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname'");
    return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
}