如何在PHP中更改密码

时间:2014-09-18 05:13:47

标签: php mysql

我在php中更改密码页面,我遇到了问题。请指导我在哪里遇到问题。

<form action="do_change_password.php" method="post">
<table width="70%" cellpadding="0" cellspacing="0" align="center" border="0">
<tr>
<td colspan="2" align="center">
<h2>CHANGE PASSWORD</h2>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="40%" height="30">Current Password
</td>
<td width="60%">
<input name="password" type="password" id="password" size="30"/>
</td>
</tr>
<tr>
<td height="30">New Password</td>
<td>
<input name="newpassword" type="password" id="newpassword" size="30"/>
</td>
</tr>
<tr>
<td height="30">Confirm New Password</td>
<td>
<input name="confirmnewpassword" type="password" id="confirmnewpassword" size="30"/>
</td>
</tr>
<tr>
<td height="30" colspan="2" align="center">
<input name="submit" type="submit" value="Submit" id="submit_btn"/>
<input name="reset" type="reset" value="Reset" id="reset"/>

</td>
</tr>
</table>
</form>

do_change_password.php

<?php
include 'includes/dbConnect.php';   

$password = $_POST['password'];
$newpassword = $_POST['newpassword'];
$confirmnewpassword = $_POST['confirmnewpassword'];

$error_msg = "Field(s) cannot be empty"; 


if ($password == '' || $newpassword == ''  ||$confirmnewpassword == '')
    {
        echo $error_msg;
        exit;
    }

$result = mysql_query("SELECT password FROM users WHERE password='$password'");

        if
        ($password != mysql_result($result< 0))
        {
            echo "Entered an incorrect password";
        }

        if($newpassword == $confirmnewpassword)
        {
  $sql = mysql_query("UPDATE registration SET password = '$newpassword' WHERE   password='$current_password'");      
        }

        if(!$sql)
        {
            echo "Congratulations, password successfully changed!";
        }
        else
        {
            echo "New password and confirm password must be the same!";
        }
 ?>

dbConnect.php

<?php
error_reporting(E_ERROR);
global $link;

$servername='localhost';
$dbname='school';
$dbusername='root';
$dbpassword='';
$table_Name="students";

$link = mysql_connect($servername,$dbusername,$dbpassword);

if (!$link) {
die('Could not connect: ' . mysql_error());
}
else 
{
 mysql_select_db($dbname,$link) or die ("could not open db".mysql_error());
}

?>  

我收到此错误,我无法更改密码:  输入错误密码恭喜,密码成功更改!无法更改密码 请告诉我我在哪里做错了。 提前致谢

5 个答案:

答案 0 :(得分:1)

我认为您在输入标记中混淆了idname

替换...

<input name="current" type="password" id="password" size="30"/>

...与...

<input id="current" type="password" name="password" size="30"/>

基本上,无论您在输入代码中指定为name,您都可以通过$_POST获得。

因此<input name="whatever" />将为$_POST['whatever']

答案 1 :(得分:1)

<input name="current" type="password" id="password" size="30" name="password"/>

$password = $_POST['password'];

$ _ POST它应该是从html标签接受name属性。

答案 2 :(得分:1)

尝试用此替换您的表单....您更改字段名称的错误,并尝试改进您的PHP代码,请参阅此SQL injection

<form action="do_change_password.php" method="post">
<table width="70%" cellpadding="0" cellspacing="0" align="center" border="0">
<tr>
<td colspan="2" align="center">
<h2>CHANGE PASSWORD</h2>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="40%" height="30">Current Password
</td>
<td width="60%">
<input name="password" type="password" id="password" size="30"/>
</td>
</tr>
<tr>
<td height="30">New Password</td>
<td>
<input name="newpassword" type="password" id="newpassword" size="30"/>
</td>
</tr>
<tr>
<td height="30">Confirm New Password</td>
<td>
<input name="confirmnewpassword" type="password" id="confirmnewpassword" size="30"/>
</td>
</tr>
<tr>
<td height="30" colspan="2" align="center">
<input name="submit" type="submit" value="Submit" id="submit_btn"/>
<input name="reset" type="reset" value="Reset" id="reset"/>

</td>
</tr>
</table>
</form>

答案 3 :(得分:1)

<form action="do_change_password.php" method="post">
<table width="70%" cellpadding="0" cellspacing="0" align="center" border="0">
<tr>
<td colspan="2" align="center">
<h2>CHANGE PASSWORD</h2>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="40%" height="30">Current Password
</td>
<td width="60%">
<input name="password" type="password" id="password" size="30"/>
</td>
</tr>
<tr>
<td height="30">New Password</td>
<td>
<input name="newpassword" type="password" id="newpassword" size="30"/>
</td>
</tr>
<tr>
<td height="30">Confirm New Password</td>
<td>
<input name="confirmnewpassword" type="password" id="confirmnewpassword" size="30"/>
</td>
</tr>
<tr>
<td height="30" colspan="2" align="center">
<input name="submit" type="submit" value="Submit" id="submit_btn"/>
<input name="reset" type="reset" value="Reset" id="reset"/>

</td>
</tr>
</table>
</form>

试试这个..它会正常工作。

答案 4 :(得分:0)

你必须抓住:

$password = $_POST['current'];
$newpassword = $_POST['new'];
$confirmnewpassword = $_POST['confirm-new'];

或替换输入的name