更改用户密码php无法正常工作

时间:2018-04-28 19:42:19

标签: php mysql

你们可以更正代码,因为它不能正常工作

这是update.php

//included session.php at the top

<?php 
    $conn_db = mysql_connect("localhost","root","") or die();
    $sel_db = mysql_select_db("registration",$conn_db) or die();
    if(isset($_POST['update_password']))
    {
    $old_password=$_POST['old_password'];
    $new_password=$_POST['new_password'];
    $confirm_password=$_POST['confirm_password'];
    $chg_pwd=mysql_query("select * from member where id=''");
    $chg_pwd1=mysql_fetch_array($chg_pwd);
    $data_pwd=$chg_pwd1['password'];
    if($data_pwd==$old_password){
    if($new_password==$confirm_password){
        $update_password=mysql_query("update member set password='$new_password' where id=''");
        echo "<script>alert('Update Sucessfully'); window.location='update.php'</script>";
    }
    else{
        echo "<script>alert('Your new and Retype Password is not match'); window.location='update.php'</script>";
    }
    }
    else
    {
    echo "<script>alert('Your old password is wrong'); window.location='update.php'</script>";
    }}
?>

<form action="" method="POST">

    <fieldset>

      <input value="" type="password" id="old_password" name="old_password" placeholder="Current Password" required>


      <input value="" type="password" id="new_password" name="new_password" placeholder="New Password" required>


      <input value="" type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password" required>

    </fieldset>
    <button id="st" name="update_password" type="submit"><span>Update</span></button>

  </form>

这是添加信息的session.php

<?php

session_start();

include('db.php');
$user_check=$_SESSION['login_user'];
$ses_sql=mysqli_query($db,"select  username,mem_id  from  member  where  
username='$user_check'  ");
$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$loggedin_session=$row['username'];
$loggedin_id=$row['mem_id'];

if(!isset($loggedin_session)  ||  $loggedin_session==NULL)
{
echo  "Go  back";
header("Location:  login.php");
}

?>

我得到“旧密码错误”,即使我输入了正确的用户密码,我认为整件事情都是垃圾。

请帮助谢谢。

新手

2 个答案:

答案 0 :(得分:0)

这是使用PDO连接的solluiotn:

<?php

    $user='root';
    $pass= '';
    $userid= 45;
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user , $pass);

    if(isset($_POST['update_password']))
    {
    $old_password=$_POST['old_password'];
    $new_password=$_POST['new_password'];
    $confirm_password=$_POST['confirm_password'];
    $stm=$dbh->query("select * from member where mem_id=45");
    $user= $stm->fetch(PDO::FETCH_ASSOC);
    $chg_pwd=$user['password'];

    if($chg_pwd==$old_password){
    if($new_password==$confirm_password){
        $update_password=$dbh->exec("update member set password='$new_password' where mem_id=45");
        echo "<script>alert('Update Sucessfully'); window.location='update.php'</script>";
    }
    else{
        echo "<script>alert('Your new and Retype Password is not match'); window.location='update.php'</script>";
    }
    }
    else
    {
    echo "<script>alert('Your old password is wrong'); window.location='update.php'</script>";
    }}
?>

<form action="" method="POST">

    <fieldset>

      <input value="" type="password" id="old_password" name="old_password" placeholder="Current Password" required>


      <input value="" type="password" id="new_password" name="new_password" placeholder="New Password" required>


      <input value="" type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password" required>

    </fieldset>
    <button id="st" name="update_password" type="submit"><span>Update</span></button>

  </form>

这是使用的注册表的模式。 enter image description here

更新密码后

enter image description here 我希望能帮助你。

答案 1 :(得分:0)

你做了一些典型的初学者错误。以下是一些建议

  • 存储纯文本密码是个坏主意。您应该在数据库中存储(盐渍)密码的哈希表示。
  • 使用mysql i * - 函数而不是mysql *(不推荐使用它们)。
  • 永远不要相信用户输入。在让它们进入数据库之前,您需要先将它们转义。
  • 实施错误处理。
那说...... 您已在注释中指定了表结构,并且您有一个名为mem_id的列,但您的查询使用id(空值)。 此查询将导致mysql错误('where子句'中的未知列'id'。)

您可以使用mysql_error()来检查此类错误。

$chg_pwd = mysql_query("select * from member where mem_id='[some_id]' ");
$error = mysql_error();

if ($error != ""){
    // you have an error in your statement
    die($error);
}

您还应该使用mem_id作为表格中的主键

ALTER TABLE `member` ADD PRIMARY KEY( `mem_id`);

当查询按预期工作时,您可以使用var_dump ($chg_pwd1);查看您真正从数据库中检索的内容,也许您希望切换到mysqli_fetch_row,因为您只希望此查询中有一行。

相关问题