我尝试使php登录,但出现此错误:Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given,我做错了什么?
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given,
register.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> • <a </div> </h3> </div> <div class="jonne"> </div> <script src="javascript/jquery.js"></script> </body> </html>
我使用000webhost,这是我第一次在线使用mysql数据库时。
您正在混合mysqli和mysql运行。
mysqli
mysql
如果您使用的是mysql函数,请mysqli_real_escape_string($your_variable);改用
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函数中* :
$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']);
注意:由于不建议使用mysql,因此请使用mysqli_ 函数。有关信息,请阅读_mysqli_