[英]Row won't update in database
I'm making a community site thing. 我正在做一个社区网站的事情。
I'm currently making the option to change your password. 我目前正在选择更改您的密码。
But, when I try to use it, it won't work.. 但是,当我尝试使用它时,它将无法工作。
Why is this? 为什么是这样? How do I fix it?
我如何解决它?
My code: 我的代码:
Settings.php (Where I change the pass) Settings.php (我在哪里更改通行证)
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:
<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>
If you need any more code, tell me. 如果您需要更多代码,请告诉我。
Thanks // Mackan90095 谢谢// Mackan90095
What error do you get? 你得到什么错误? Or does it never get inside the if($num==1)?
还是永远不会进入if($ num == 1)?
Maybe its smart to retrieve the users id and use that to change the password. 也许检索用户ID并使用它来更改密码很聪明。 Imagine two users having the same username, that would change both their passwords.
想象两个用户使用相同的用户名,这将同时更改两个密码。
I would also include a password confirmation field to make sure they enter the new password twice (just as a precaution). 我还将包括一个密码确认字段,以确保他们两次输入新密码(以防万一)。
Useful site: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers 有用的网站: http : //wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Why dont you try using try catch to see the error and replace if($result==1) with if($result) 为什么不尝试使用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();
}
You have a beautiful code, but I've seen only this: 您有一个漂亮的代码,但我仅看到以下内容:
change 更改
<form action="settings.php" method="POST">
to 至
<form action="Settings.php" method="POST">
Settings.php is capitalized? Settings.php是大写的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.