gpt4 book ai didi

php - mysql 和 mysqli 的问题

转载 作者:行者123 更新时间:2023-11-28 15:39:31 25 4
gpt4 key购买 nike

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

Notice: Undefined variable: conn in C:\xampp\htdocs\swift\core\functions\users.php on line 85

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\swift\core\functions\users.php on line 85

Fatal error: Uncaught Error: Call to undefined function mysqli_result() in C:\xampp\htdocs\swift\core\functions\users.php:86 Stack trace: #0 C:\xampp\htdocs\swift\loginact.php(14): f_exists('') #1 {main} thrown in C:\xampp\htdocs\swift\core\functions\users.php on line 86

我不断收到这些错误,但我找不到方法,我已经尝试了所有方法.. 希望有人能帮助我,因为我是 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--------------------

<?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 和其他编程语言的知识。即使我现在问,我也会继续寻找答案。非常感谢。

最佳答案

我怀疑你尝试了一切!

看起来您正试图从 mysql_ 函数迁移到 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 函数的内容开始,然后按照并尝试翻译手册中的示例。

阅读错误消息实际上很有帮助。但您可能会不知所措,因为您面临着许多问题。

Call to undefined function mysqli_result()

那是因为没有mysqli_result函数。

关于php - mysql 和 mysqli 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50389205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com