本文介绍了mysqli_real_escape_string() 需要 2 个参数,1 个给定致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到这个错误,我在修复这个错误时遇到了问题,我在 PHP 方面并不擅长,因为我还在学习.我正在处理注册表并使用 PHP 5.6.我查看了有关较旧问题的其他答案,但没有成功.

I keep getting this errors and I am having problems fixing that, am not good in PHP because am still learning. I am working on a registration form and am using PHP 5.6. I have looked at other answers on older questions but I haven't succeeded.

这是我的代码:

<?php
session_start();
if (isset($_SESSION['user']) != "") {
    header("Location: index.html");
}
include_once 'dbconnect.php';

if (isset($_POST['signup'])) {
    $fname  = mysqli_real_escape_string($_POST['fullname']);
    $tphone = mysqli_real_escape_string($_POST['telephone']);
    $uemail = mysqli_real_escape_string($_POST['email']);
    $urole  = mysqli_real_escape_string($_POST['role']);
    $upass  = md5(mysqli_real_escape_string($_POST['upass']));

    $uname  = trim($uname);
    $tphone = trim($tphone);
    $email  = trim($email);
    $urole  = trim($role);
    $upass  = trim($upass);

    // email exist or not
    $query  = "SELECT email FROM users WHERE email='$uemail'";
    $result = mysqli_query($query);

    $count = mysqli_num_rows($result); // if email not found then register

    if ($count == 0) {

        if (mysqli_query("INSERT INTO users(firstname,telephone,email,role,pass) VALUES('$fname','$tphone','$uemail','$urole',$upass')")) {
?>
           <script>alert('successfully registered ');</script>
            <?php
        } else {
?>
           <script>alert('error while registering you...');</script>
            <?php
        }
    } else {
?>
           <script>alert('Sorry Email ID already taken ...');</script>
            <?php
    }

}
?>

我不断收到的错误是:

警告:mysqli_real_escape_string() 需要 2 个参数,1 个在 C:Apache24htdocsTimewiselandinglogin.php 第 12 行

警告:mysqli_real_escape_string() 需要 2 个参数,1 个在 C:Apache24htdocsTimewiselandinglogin.php 第 13 行

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:Apache24htdocsTimewiselandinglogin.php on line 13

警告:mysqli_query() 需要至少 2 个参数,其中 1 个在 C:Apache24htdocsTimewiselandinglogin.php 第 18 行

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:Apache24htdocsTimewiselandinglogin.php on line 18

警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,C:Apache24htdocsTimewiselandinglogin.php 第 19 行给出的 null

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:Apache24htdocsTimewiselandinglogin.php on line 19

警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,C:Apache24htdocsTimewiselandinglogin.php 第 21 行给出的 null

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:Apache24htdocsTimewiselandinglogin.php on line 21

你能帮我解决这个问题吗,我需要知道我应该如何实际解决这个问题.

Can you please help me on this, I need to know how I should fix this practically.

推荐答案

技术上回答这个问题,根据手册,这两个函数都需要传递一个数据库连接并作为第一个参数:

To technically answer this, both of these functions require a db connection be passed and as the first parameter, as per the manuals:

然后在评论中声明您正在使用 PDO 进行连接.

Then in comments you state that you are using PDO to connect with.

那些不同的 MySQL API 不会混合使用.从连接到查询,您需要使用相同的方法.因此,如果您想继续使用 PDO 连接,您将需要使用 PDO 函数来查询,而不是 mysqli_*.

Those different MySQL APIs do not intermix. You need to use the same one from connecting to querying. Therefore, if you want to continue to use a PDO connection, you will need to use the PDO functions to query with and not mysqli_*.

  • Start with the manual: http://php.net/manual/en/book.pdo.php it's all in there.

对于 PDO 准备好的语句:

And for PDO prepared statements:

还要检查错误:

  • http://php.net/manual/en/pdo.error-handling.php (PDO)
  • http://php.net/manual/en/function.error-reporting.php (PHP)

密码

我还注意到您正在尝试存储密码 MD5.不建议这样做,因为将其用作密码存储功能不再被认为是安全的.

I also noticed that you are attemtpting to store passwords MD5. This is not recommended as it is no longer considered safe to use as a password storing function.

  • 如果您打算使用此功能进行直播,不要.

使用以下方法之一:

其他链接:

关于列长度的重要旁注:

如果您决定使用 password_hash() 或 crypt,请务必注意,如果您当前密码列的长度小于 60,则需要将其更改为(或更高).手册建议长度为 255.

If and when you do decide to use password_hash() or crypt, it is important to note that if your present password column's length is anything lower than 60, it will need to be changed to that (or higher). The manual suggests a length of 255.

您需要更改列的长度并使用新的散列重新开始以使其生效.否则,MySQL 将静默失败.

You will need to ALTER your column's length and start over with a new hash in order for it to take effect. Otherwise, MySQL will fail silently.

我也说过:

if (isset($_SESSION['user']) != "") 会给你一个误报.

语法是:if isset AND equals to,而不是if isset equals to,这是它目前被解释为的内容.

The syntax is: if isset AND equals to, and not if isset equals to which is what it is presently being interpreted as.

使用:

if (isset($_SESSION['user']) && $_SESSION['user'] != "")

关于你的 POST 数组.

In regards to your POST arrays.

确保您使用的 HTML 表单确实使用了 POST 方法,并且所有元素都拥有各自的名称属性.

Make sure the HTML form you are using does use a POST method and that all elements hold their respective name attributes.

即:

请注意,name="fullname"name="FullName" 是两种不同的动物.

Note that name="fullname" and name="FullName" are two different animals.

  • 那些区分大小写.

还建议在每个header后面加上exit;,否则你的代码可能想继续执行.

It is also suggested to add exit; after each header, otherwise your code may want to continue to execute.

header("Location: index.html");
exit;

这篇关于mysqli_real_escape_string() 需要 2 个参数,1 个给定致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:25