你们可以更正代码,因为它不能正常工作
这是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");
}
?>
我得到“旧密码错误”,即使我输入了正确的用户密码,我认为整件事情都是垃圾。
请帮助谢谢。
答案 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>
更新密码后:
答案 1 :(得分:0)
你做了一些典型的初学者错误。以下是一些建议
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
,因为您只希望此查询中有一行。