行不会在数据库中更新

时间:2013-11-29 13:10:51

标签: php mysql pdo

我正在制作一个社区网站。

我目前正在选择更改密码。

但是,当我尝试使用它时,它将不起作用..

这是为什么?我该如何解决?

我的代码:

Settings.php (我在哪里更改通行证)

PHP:

<?php
error_reporting(E_ALL);
include_once('includes/connection.php');
include_once('includes/user.php');
if(isset($_SESSION['logged_in'])){
    if(isset($_POST['oldpass']) and isset($_POST['newpass'])){
        $name = $_COOKIE['name'];
        $oldpass = md5($_POST['oldpass']);
        $newpass = md5($_POST['newpass']);

        $query = $pdo->prepare("SELECT * FROM users WHERE username=? AND password=?");

            $query->bindValue(1, $name);
            $query->bindValue(2, $oldpass);

            $query->execute();

            $num = $query->rowCount();

            if($num==1){
                $query = $pdo->prepare("UPDATE users SET password=? WHERE username=?");

                $query->bindValue(1, $newpass);
                $query->bindValue(2, $name);

                $result = $query->execute();
                if($result==1){
                    header('Location: logout.php');
                }else{
                    echo "Something went wrong.";
                }
            }
    }
}
?>

HTML:

<html>
    <head>
        <title>MackNet</title>
        <link rel="stylesheet" type="text/css" href="assets/style.css">
    </head>
    <body>
        <div id="main">
        <?php 
            $name = $_COOKIE['name'];
            $pass = $_COOKIE['pass'];
            $user = new User();
            $row = $user->fetch_all($name, $pass);
         ?>
            <div id="toolbar">
                <?php 
                    echo " <a href='main.php'>Home</a> ";
                    echo " <a href='logout.php'>Logout</a> ";
                    echo " <a href='settings.php'>Settings</a> ";
                    if($row['group'] == 2){
                        echo " <a href='users.php'>Manage Users</a> ";
                    }
                ?>
                <hr>
            </div>
            <form action="settings.php" method="POST">
                <input type="password" name="oldpass" placeholder="Old Password">
                <input type="password" name="newpass" placeholder="New Password">
                <input type="submit" value="Change password">
            </form>
        </div>
    </body>
</html>

如果您需要更多代码,请告诉我。

谢谢// Mackan90095

3 个答案:

答案 0 :(得分:0)

你得到什么错误?或者它永远不会进入if($ num == 1)?

也许它可以智能地检索用户ID并使用它来更改密码。想象一下两个用户具有相同的用户名,这将改变他们的密码。

我还会包含一个密码确认字段,以确保他们输入两次新密码(仅作为预防措施)。

有用的网站:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

答案 1 :(得分:0)

为什么不尝试使用try catch查看错误并将if($ result == 1)替换为if($ result)

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try{
    $query = $pdo->prepare("SELECT * FROM users WHERE username=? AND password=?");

        $query->bindValue(1, $name);
        $query->bindValue(2, $oldpass);

        $query->execute();

        $num = $query->rowCount();

        if($num==1){
            $query = $pdo->prepare("UPDATE users SET password=? WHERE username=?");

            $query->bindValue(1, $newpass);
            $query->bindValue(2, $name);

            $result = $query->execute();
            if($result)
                header('Location: logout.php');
        }
    } 
catch (PDOException $e) {
      echo "DataBase Error: ".$e->getMessage();
    } 
catch (Exception $e) {
      echo "General Error: ".$e->getMessage();
    }

答案 2 :(得分:0)

你有一个漂亮的代码,但我只见过这个:

更改

<form action="settings.php" method="POST">

<form action="Settings.php" method="POST">

Settings.php已大写?