mysql和mysqli的问题

xzv2uavs  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(257)

警告:mysqli\u real\u escape\u string()需要2个参数,其中1个参数在第49行的c:\xampp\htdocs\swift\core\functions\general.php中给出
注意:未定义的变量:conn在c:\xampp\htdocs\swift\core\functions\users.php的第85行
警告:mysqli\u query()要求参数1为mysqli,在第85行的c:\xampp\htdocs\swift\core\functions\users.php中给出null
致命错误:未捕获错误:调用c:\xampp\htdocs\swift\core\functions\users中未定义的函数mysqli\u result()。php:86 stack 跟踪:#0 c:\xampp\htdocs\swift\loginact.php(14):f#u exists('')#1{main}在第86行的c:\xampp\htdocs\swift\core\functions\users.php中抛出
我不断地犯这些错误,但我找不到办法,我已经尝试了一切。。希望有人能帮助我,因为我是新的php。我已经做了差不多一天了,我想不出答案。
这是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

np8igboo

np8igboo1#

注意:未定义的变量: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; 
}
vyswwuz2

vyswwuz22#

我怀疑你什么都试过了!
看起来好像您正在尝试从mysql\函数迁移到mysqli\函数。
从mysqli\u fetch\u 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\u id\u开始,然后按照手册的示例进行翻译。
错误消息实际上非常有用。但你很可能会不知所措,因为你面临着许多挑战。
调用未定义的函数mysqli\u result()
这是因为没有mysqli\u result函数。

carvr3hs

carvr3hs3#

1-general.php第49行:查看mysqli\u real\u escape\u string,在过程代码中,必须指定两个参数,链接(或连接)和字符串。

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\u 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点相同。

xdyibdwo

xdyibdwo4#


<?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有点难,我已经找了差不多一天的答案了。我只是一个新的编程和我正在尽我最大的努力学习这类事情希望有人能帮助我。
我希望我能在这里学到更多关于php和其他编程语言的知识。。即使我现在在问,我也会继续寻找答案。。非常感谢。

相关问题