mysql_real_escape_string() expects parameter 2 to be resource, object given in
代码如下:
<?php
$page_title = 'Register ';
include("header.html"); #网页头部
?>
<?php
if(isset($_POST['submitted'])){
require_once("mysqli_connect.php");
$error=array(); #定义错误为数组
if (empty($_POST['first_name'])){
$error[]='you forgot to enter you first name';
}else{
$fn= mysql_real_escape_string(trim($_POST['first_name']),$dbc);
} #first name 条件语句
if (empty($_POST['last_name'])) {
$error[]='you forgot to enter you last name';
}else{
$ln= mysql_real_escape_string(trim($_POST['last_name']),$dbc);
} #last name 条件语句
if (empty($_POST['email'])){
$error[]='you forgot to enter you email';
}else{
$e= mysql_real_escape_string(trim($_POST['email']),$dbc);
} # email 条件语句
if (!empty($_POST['password1'])) {
if($_POST['password1']!=$_POST['password2']){
$error[]='your password did not match the confirmed password.';
}else{
$p= mysql_real_escape_string(trim($_POST['password1']),$dbc);
}
}else{
$error[]='you forgot to your password';
} # password 条件语句
if(empty($error)){
$q="INSERT INTO users (first_name, last_name, email, pass,registration_date) VALUES ('$fn','$ln','$e',SHA1('$p'),now())";
$r=@mysqli_query ($dbc ,$q);
if($r){
echo ' <h1>Thank you !</h1>
<p> you are now registered. </p>';
}else{
echo "<h1> system error</h1>
<p> you could not bi registered due to a system error . We apolagize for any inconvenience. mysqli_error:.mysqli_error($dbc).<br />Query:.$q.</p>";
}
mysqli_close($dbc);
include('footer.html');
exit();
}else{
echo" <h1 > error!</h1><p> the following error occurrde :</p>" ;
foreach($error as $key =>$msg){
echo "the error at $key is $msg <br />";
}
echo"<p><h1>please try again </h1></p>";
} #错误显示
}
?>
<h1 align="center"> Register </h1>
<div style="width:750px; border-top:#000 solid 1px; margin:20px auto; ">
<fieldset><legend>please do it </legend>
<form action="register.php" method="post" > <!--表单内容-->
<p>First_name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php if(isset($_POST[first_name])) echo $_POST[first_name]?>" > </p>
<p>Last_name: <input type="text" name="last_name" size="15" maxlength="40" value="<?php if(isset($_POST[last_name])) echo $_POST[last_name]?>" > </p>
<p>Email Address: <input type="text" name="email" size="15" maxlength="60" value="<?php if(isset($_POST[email])) echo $_POST[email]?>" > </p>
<p>Password: <input type="password" name="password1" size="20" maxlength="20" > </p>
<p>Confirm password: <input type="password" name="password2" size="20" maxlength="20" > </p>
<p><input type="submit" name="submit" value="Register"> </p>
<p ><input type="hidden" name="submitted" value="TRUE"> </p>
</form>
</fieldset>
</div>
<?php
include ("footer.html");
?>
为什么注册后显示:(省下了网页 header 和 footer)
Warning: mysql_real_escape_string() expects parameter 2 to be resource, object given in E:\wamp\www\dance of life\mysqli_connection\register.php on line 16
Warning: mysql_real_escape_string() expects parameter 2 to be resource, object given in E:\wamp\www\dance of life\mysqli_connection\register.php on line 22
Warning: mysql_real_escape_string() expects parameter 2 to be resource, object given in E:\wamp\www\dance of life\mysqli_connection\register.php on line 28
Warning: mysql_real_escape_string() expects parameter 2 to be resource, object given in E:\wamp\www\dance of life\mysqli_connection\register.php on line 34
Thank you !
you are now registered.
既然已经成功注册,为什么还弹出Warning?
补充一下 数据库 mysqli_connect.php 能正常连接
[解决办法]
你可以直接把第二个参数去掉,这个是可选的。如果省略则直接使用上一个连接。
这样:mysql_real_escape_string(trim($_POST['last_name']));
[解决办法]
你用的是mysqli吧! mysql_real_escape_string() 必须要用在有mysql_connect()的地方。
如果想用mysqli,就需要把代码 mysql_real_escape_string($_POST['email'], $dbc);
换成: mysqli_real_escape_string($dbc, $_POST['email']);
[解决办法]
mysql_real_escape_string(string,connection)
第二个参数可选的,是连接资源集,而你却传了对象。缺省试试。
[解决办法]
require_once("mysqli_connect.php");里面的mysqli_conenct返回值就是资源。