php - 发出警告 : mysqli_real_escape_string() expects exactly 2 parameters, 1...我做错了什么?

标签 php mysqli

<分区>

我尝试使 php 登录,但出现此错误:警告:mysqli_real_escape_string() 需要正好 2 个参数,1 个给定,我做错了什么?

注册.php

<!doctype html>
<html lang"fi">
<head>
<link rel="icon" type='image/png' href='images/logo.png'>
<title>
asd
</title>
<link href="css/styles.css" type="text/css" rel="stylesheet">
</head>
<body>
<!--reg alkaa-->
<form action="register.php" method="post">
<p><input type="text" name="username" placeholder="Username">
<p><input type="email" name="email" placeholder="Email">
<p><input type="password" name="pass" placeholder="Password">
<p><input type="password" name="pass1" placeholder="Password">
<p><input type="submit" name="submit" value="Register">
</form>
<?php

if(isset($_POST['submit']))
{
$username = mysqli_real_escape_string($_POST['username']);
$pass = mysqli_real_escape_string($_POST['pass']);
$pass1 = mysqli_real_escape_string($_POST['pass1']);
$email = mysqli_real_escape_string($_POST['email']);
if($username && $pass && $pass1 && $email)
{
if($pass==$pass1)
{
    $connect = mysql_connect("mysql.example.com","username","password");
    mysql_select_db("my_database");
    $query = mysql_query("INSERT INTO users VALUES('$username','$pass','$email');");
    echo "You have been registered.";
}
else
{
    echo "Password must match.";
}
}
else
{
echo "All fields are required.";
}
}
 ?>
<!--reg end-->
<Center>
<a href="index.php">
<h1>
asd
</h1>
</center>
<div id="main">
<h3>
 <div class="menu"> <a href="index.php">Etusivu</a> &bullet; 
 <a                                       </div>
</h3>
</div>
<div class="jonne"> 
</div>
<script src="javascript/jquery.js"></script>
</body>
</html>

我使用000webhost,这是我第一次在线使用mysql数据库。

最佳答案

您正在混合使用 mysqlimysql 函数。

如果您使用的是 mysql 函数,则改为 mysqli_real_escape_string($your_variable); 使用

$username = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['pass']);
$pass1 = mysql_real_escape_string($_POST['pass1']);
$email = mysql_real_escape_string($_POST['email']);

如果您使用 mysqli_* 函数,那么您必须将与数据库的连接包含到 mysqli_real_escape function:

$username = mysqli_real_escape_string($your_connection, $_POST['username']);
$pass = mysqli_real_escape_string($your_connection, $_POST['pass']);
$pass1 = mysqli_real_escape_string($your_connection, $_POST['pass1']);
$email = mysqli_real_escape_string($your_connection, $_POST['email']);

注意:使用 mysqli_* 函数,因为 mysql 已被弃用。有关信息,请阅读 mysqli_ *

关于php - 发出警告 : mysqli_real_escape_string() expects exactly 2 parameters, 1...我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25636975/

相关文章:

php - 尝试从 azure blob 下载文件,mime 类型有问题

javascript - PHP MySQL JavaScript

PHP-MySQL : Returning Query as Array index name and value

PHP 从多对多获取

php - 在 PHP 中,如何设置默认的 PDO 获取类?

phpMyAdmin fatal error : Call to undefined function __()

php - 数组未从数据库输入数据

php - 从 MYSQL 更新到 MYSQLI

php - MySQIi 选择计数并按计数排序

php - PHP 中的 OOP - "__construct"连接