我刚开始学习 php 和 sql,所以请放轻松,我知道我在某些地方会出错。我正在尝试允许用户登录并能够更改他们的密码。我尝试了一个我认为应该可以工作的脚本,但我想我做错了什么,因为它只会链接到 php 函数页面,根本不会更改密码。这是我的脚本:
HTML 表单:
<form method="POST" action="includes/changepassword.php">
<p><input type="password" name="oldpasswd" id="oldpasswd" maxlength="30" placeholder="Old Password"></p>
<p><input type="password" name="newpsswd1" id="newpsswd1" maxlength="30" placeholder="New Password"></p>
<p><input type="password" name="newpsswd2" id="newpsswd2"maxlength="30" placeholder="Confirm Password"></p>
<input type="submit" name="submit" id="submit" value="change password">
更改密码.php 文件:
<?php
require_once("session.php");
require_once("functions.php");
require('_config/connection.php');
function changepassword ($oldpasswd, $newpasswd1, $newpasswd2) {
/*
* RETURNS
* 0 - if password changed
* 1 - if new passwords are not equal
* 2 - if user authentification problems
*/
$oldpasswd = ($_POST['oldpasswd']);
$newpasswd1 = ($_POST['newpasswd1']);
$newpasswd1 = ($_POST['newpasswd2']);
if ($newpasswd1 != $newpasswd2) {
return 1;
}
//check user logged in changes OWN passwd
$sql = "SELECT password FROM ptb_users WHERE id = ".$_SESSION['user_id'];
$result = mysql_query($sql)or die('User not found: ' . mysql_error());
if (md5($oldpasswd)==$result) {
//Encrypt $emailpassword in MD5 format for the database
$md5_np=md5($newpasswd1);
// Make a safe query
$query = sprintf("UPDATE `ptb_users` SET `password` = '%s'
WHERE `id` = ".$_SESSION['user_id'],
mysql_real_escape_string($md5_np));
mysql_query($query)or die('Could not update password: ' . mysql_error());
return 0;
} else {
return 2;
}
}
?>
我做错了什么?