更改密码代码错误

时间:2010-03-18 10:18:46

标签: php mysql

我已经创建了一个更改密码的代码。现在它似乎包含一个错误。当我填写表单来更改密码,然后单击保存错误消息:

  

警告:mysql_real_escape_string()期望参数2为资源,在第103行的C:\ Program Files \ xampp \ htdocs \ e-Complaint(FYP)\ userChangePass.php中给出null

     

警告:mysql_real_escape_string()期望参数2为资源,在第103行的C:\ Program Files \ xampp \ htdocs \ e-Complaint(FYP)\ userChangePass.php中给出null

我真的不知道错误信息的含义。拜托,拜托。帮我解决一下。

这是代码:

<?php session_start(); ?>
<?php # change password.php

//set the page title and include the html header.
$page_title = 'Change Your Password';
//include('templates/header.inc');

if(isset($_POST['submit'])){//handle the form
    require_once('connectioncomplaint.php');//connect to the db.
    //include "connectioncomplaint.php";

    //create a function for escaping the data.
    function escape_data($data){
        global $dbc;//need the connection.
        if(ini_get('magic_quotes_gpc')){
            $data=stripslashes($data);
        }
        return mysql_real_escape_string($data, $dbc);
    }//end function

    $message=NULL;//create the empty new variable.

    //check for a username
    if(empty($_POST['userid'])){
        $u=FALSE;
        $message .='<p> You forgot enter your userid!</p>';
    }else{
        $u=escape_data($_POST['userid']);
    }

    //check for existing password
    if(empty($_POST['password'])){
        $p=FALSE;
        $message .='<p>You forgot to enter your existing password!</p>';
    }else{
        $p=escape_data($_POST['password']);
    }

    //check for a password and match againts the comfirmed password.
    if(empty($_POST['password1'])) {
        $np=FALSE;
        $message .='<p> you forgot to enter your new password!</p>';
    }else{
        if($_POST['password1'] == $_POST['password2']){
        $np=escape_data($_POST['password1']);
    }else{
        $np=FALSE;
        $message .='<p> your new password did not match the confirmed new password!</p>';
    }
}

if($u && $p && $np){//if everything's ok.

    $query="SELECT userid FROM access WHERE (userid='$u' AND password=PASSWORD('$p'))";
    $result=@mysql_query($query);
    $num=mysql_num_rows($result);
    if($num == 1){
        $row=mysql_fetch_array($result, MYSQL_NUM);

        //make the query
        $query="UPDATE access SET password=PASSWORD('$np') WHERE userid=$row[0]";
        $result=@mysql_query($query);//run the query.
        if(mysql_affected_rows() == 1) {//if it run ok.

            //send an email,if desired.
            echo '<p><b>your password has been changed.</b></p>';
            include('templates/footer.inc');//include the HTML footer.
            exit();//quit the script.

        }else{//if it did not run OK.
            $message= '<p>Your password could not be change due to a system error.We apolpgize for any inconvenience.</p><p>' .mysql_error() .'</p>';
            }
        }else{
            $message= '<p> Your username and password do not match our records.</p>';
        }
        mysql_close();//close the database connection.

    }else{
        $message .='<p>Please try again.</p>';
    }
}//end oh=f the submit conditional.

//print the error message if there is one.
if(isset($message)){
    echo'<font color="red">' , $message, '</font>';
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">





<body>
<script language="JavaScript1.2">mmLoadMenus();</script>
<table width="604" height="599" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="130" colspan="7"><img src="images/banner(E-Complaint)-.jpg" width="759" height="130" /></td>
  </tr>
  <tr>
    <td width="100" height="30" bgcolor="#ABD519"></td>
    <td width="100" bgcolor="#ABD519"></td>
    <td width="100" bgcolor="#ABD519"></td>
    <td width="100" bgcolor="#ABD519"></td>
    <td width="100" bgcolor="#ABD519"></td>
    <td width="160" bgcolor="#ABD519">
    <?php include "header.php"; ?>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="7" bgcolor="#FFFFFF">

<fieldset><legend> Enter your information in the form below:</legend>

<p><b>User ID:</b> <input type="text" name="username" size="10" maxlength="20" value="<?php if(isset($_POST['userid'])) echo $_POST['userid']; ?>" /></p>

<p><b>Current Password:</b> <input type="password" name="password" size="20" maxlength="20" /></p>

<p><b>New Password:</b> <input type="password" name="password1" size="20" maxlength="20" /></p>

<p><b>Confirm New Password:</b> <input type="password" name="password2" size="20" maxlength="20" /></p>
</fieldset>

<div align="center"> <input type="submit" name="submit" value="Change My Password" /></div>

</form><!--End Form-->

    </td>

  </tr>
</table>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

更改

return mysql_real_escape_string($data, $dbc);

return mysql_real_escape_string($data);

您的$ dbc不是资源,它是null。

答案 1 :(得分:0)

由于只有一次出现mysql_real_escape_string()并且它依赖全局变量$ dbc来设置为mysql连接资源试试。

function escape_data($data){
  global $dbc;//need the connection.
  if ( !is_resource($dbc) ) {
    die('database connection resource not initialized');
  }

  if(ini_get('magic_quotes_gpc')){
    $data=stripslashes($data);
  }

  return mysql_real_escape_string($data, $dbc);
}