简体   繁体   English

mysqli连接不适用于mysqli_real_escape_string

[英]mysqli connection not working with mysqli_real_escape_string

I have a simple demo php scripts am trying out. 我有一个简单的演示php脚本正在尝试。 i have been using the deprecated mysql_ extensions earlier, trying to migrate from that to mysqli_ is being overly buggy. 我以前一直在使用不推荐使用的mysql_扩展,试图从该版本迁移到mysqli_实在是过分的错误。 when trying to use mysqli_real_escape_string php throws a variable not defined error cant seem to figure out why. 当试图使用mysqli_real_escape_string php抛出a variable not defined error似乎无法弄清原因。

    <?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bridgoo";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection
if ($conn->connect_error) {
  //do stuff
  }
// prepare and bind
$stmt = $conn->prepare("INSERT INTO bridgoo_users (username, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $password, $email);


    //generate password hash using blowfish algorithm
   $password_hash =  password_hash($_POST['password'], PASSWORD_BCRYPT, ["cost" => 9]);

$username = clean_input($_POST['username']);
$password = clean_input($password_hash);
$email = clean_input($_POST['email']);


function clean_input($input){

    $input = htmlentities($input);
    $input = htmlspecialchars($input,ENT_QUOTES);
    $input = mysqli_real_escape_string($conn,$input);

    return $input;
}


$stmt->execute();
$stmt->close();
$conn->close();

?>

php error Notice: Undefined variable: conn in C:\\xampp\\server1\\htdocs\\bridgoo\\inc\\register.php on line 24 php错误通知:未定义变量:第24行的C:\\ xampp \\ server1 \\ htdocs \\ bridgoo \\ inc \\ register.php中的conn

The error about undefined variable $conn has to do with PHP variable scope , and other people have answered that. 关于undefined variable $conn的错误与PHP变量scope有关 ,其他人已经回答了。

But your code needs some other suggestions too. 但是您的代码也需要其他一些建议。

You don't need to clean inputs at all if you use query parameters. 如果使用查询参数,则根本不需要清除输入。 That's the point of using query parameters: No need for mysqli_real_escape_string() because parameters are automatically safe. 这就是使用查询参数的要点:不需要mysqli_real_escape_string()因为参数是自动安全的。

In fact, you must not use mysqli_real_escape_string() , because your data will be inserted to your database literally with \\' escaped apostrophes. 实际上,您绝对不能使用mysqli_real_escape_string() ,因为您的数据将使用\\'转义撇号按字面意义插入到数据库中。

Simply put: SQL parameters and mysqli_real_escape_string() are mutually exclusive. 简而言之: SQL参数和mysqli_real_escape_string()是互斥的。 Don't do both. 不要两者都做。

Also, it makes no sense to use htmlentities() or htmlspecialchars () at all for sanitizing SQL inputs, even if you use mysqli_real_escape_string() instead of parameters. 另外,即使使用mysqli_real_escape_string()而不是参数,也完全没有必要使用htmlentities()htmlspecialchars ()mysqli_real_escape_string() SQL输入。

Those html-related functions are for HTML output only. 这些与html相关的功能仅用于HTML输出。 They're very important for protecting your web app against XSS vulnerabilities , which is another unrelated web security risk you need to know about. 它们对于保护Web应用程序免受XSS漏洞非常重要,这是您需要了解的另一种无关的Web安全风险。 But they're not needed for sanitizing SQL input. 但是清理SQL输入并不需要它们。

Other comments: 其他的建议:

  • It's confusing that you're re-using username and password variables for both the mysqli connection and the application data. 令人困惑的是,您在mysqli连接和应用程序数据中都重新使用了用户名和密码变量。 There's no reason to re-use variables, they don't cost you anything. 没有理由重复使用变量,它们不会花费您任何费用。
  • Make sure the order of parameters in your INSERT matches the order of bind variables you pass to bind_param() . 确保INSERT中参数的顺序与传递给bind_param()的绑定变量的顺序匹配。
  • Always check the return value of prepare() and execute() . 始终检查prepare()execute()的返回值。 They return FALSE if they fail. 如果失败,则返回FALSE。 If they fail, you must log the error and report to the user that the page didn't work. 如果失败,则必须记录该错误,并向用户报告该页面无效。

    I prefer to log the technical error message to the PHP error log file, and report something more friendly to the user. 我更喜欢将技术错误消息记录到PHP错误日志文件中,并向用户报告更友好的内容。

    Get into the habit of keeping a window open to watch your PHP error log during development, it'll help you a lot. 养成在开发过程中保持窗口打开以查看PHP错误日志的习惯,这对您有很大帮助。

Here's how I suggest you write your code: 我建议您按照以下方式编写代码:

<?php

$mysql_servername = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_dbname = "bridgoo";

$conn = new mysqli($mysql_servername, $mysql_username, $mysql_password, $mysql_dbname);

if ($conn->connect_error) {
  error_log($conn->connect_error);
  die("Sorry, a database error occurred.");
}

$stmt = $conn->prepare("
  INSERT INTO bridgoo_users (username, password, email) 
  VALUES (?, ?, ?)");
if ($stmt === false) {
  error_log($conn->error);
  die("Sorry, a database error occurred.");
}

$stmt->bind_param("sss", $username, $password_hash, $email);

//generate password hash using blowfish algorithm
$password_hash = password_hash($_POST['password'], PASSWORD_BCRYPT, ["cost" => 9]);

$username = $_POST['username'];
$email = $_POST['email'];

if ($stmt->execute() === false) {
  error_log($conn->error);
  die("Sorry, a database error occurred.");
}

if ($stmt->affected_rows == 1) {
  echo "Success!"
} else {
  echo "Sorry, an unknown problem occurred, and your record was not saved."
}

Your clean_input function does not have access to your $conn variable, it lives in a different scope . 您的clean_input函数无法访问$conn变量,它位于其他作用域内

The easy solution would be to add the connection as a parameter to the function. 一种简单的解决方案是将连接作为参数添加到函数中。 Something like this: 像这样:

<?php
...


$username = clean_input($_POST['username'], $conn);
$password = clean_input($password_hash, $conn);
$email = clean_input($_POST['email'], $conn);


function clean_input($input, $conn){

...

Wether that is the best or cleanest solution I'll leave in the middle, but it should work just fine. 最好的还是最干净的解决方案,我将放在中间,但是它应该可以正常工作。

You should pass $conn as parameter to your function clean_input ; 您应该将$conn作为参数传递给函数clean_input

$username = clean_input($conn, $_POST['username']);
$password = clean_input($conn, $password_hash);
$email = clean_input($conn, $_POST['email']);


function clean_input($conn, $input){

    $input = htmlentities($input);
    $input = htmlspecialchars($input,ENT_QUOTES);
    $input = mysqli_real_escape_string($conn,$input);

    return $input;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM