相同的形式,但有输入做其他事情 [英] Same form but have input do something else

查看:47
本文介绍了相同的形式,但有输入做其他事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚创建了一个changedetails.php"页面,用户可以在其中更改他们的详细信息,例如密码"和电子邮件".它们都工作成功,但我试图允许用户更改他们的电子邮件而不必更改他们的密码.

所以基本上,如果用户只想更改他们的电子邮件,它将在数据库中更新,而无需更改他们的密码.

代码:

Honda |<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><link href='http://fonts.googleapis.com/css?family=Julius+Sans+One' rel='stylesheet' type='text/css'><link href="../css/style.css" rel="stylesheet" type="text/css" media="all"/><?phpsession_start();$username = $_SESSION['sess_user'];echo '<div class="search1"><h2>'.$username.'</h2></div>';如果(isset($_SESSION['sess_user'])){//用户已登录if (isset($_POST['submit'])) {//开始修改密码//检查字段$oldpassword = md5($_POST['oldpassword']);$newpassword = md5($_POST['newpassword']);$email = $_POST['email'];$repeatnewpassword = md5($_POST['repeatnewpassword']);//根据数据库检查密码include('../includes/config.php');$queryget = mysql_query("选择密码从登录WHERE username='$用户名'")或死(更改密码失败");$row = mysql_fetch_assoc($queryget);$oldpassworddb = $row['password'];//检查密码如果($oldpassword==$oldpassworddb){//检查两个新密码如果($newpassword==$repeatnewpassword){//成功//修改数据库中的密码$querychange = mysql_query("更新登录设置密码='$新密码'WHERE username='$用户名'");$querychange = mysql_query("更新登录设置电子邮件='$电子邮件'WHERE username='$用户名'");die("

您的密码已被更改.<br><br>返回

");} 别的 {die("<div class='results'>新密码不匹配!</div>");}} 别的 {die("<div class='results'>旧密码不匹配!</div>");}} 别的 {echo "

";echo "<label>旧密码:</label> <input type='password' id='password' name='oldpassword'>";echo "<p><label>新密码:</label>";echo "<input type='password' id='password' name='newpassword'>";echo "<p><label>重复新密码:</label>";echo "<input type='password' name='repeatnewpassword'><p>";echo "<label>Email:</label> <input type='email' name='email'>";echo "<p><input type='submit' name='submit' class='submit' value='submit'>";echo "<br><br><br><h2><p><a href='index2.php'>返回</a></p></h2><;/表格>";}} 别的 {die("您必须登录才能更改密码");}?><img src="../images/main.jpg">

谢谢!

更新 2:

if ($newpassword != "") {//检查密码如果($oldpassword==$oldpassworddb){//检查两个新密码如果($newpassword==$repeatnewpassword){//成功//修改数据库中的密码$querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE username='$username'");}别的die("<div class='results'>新密码不匹配!</div>");}别的die("<div class='results'>旧密码不匹配!</div>");}}如果 ($email != "") {$querychange = mysql_query("UPDATE login SET email='$email' WHERE username='$username'");die("<div class='successmate'>您的密码已更改.<a href='index2.php'><br><br> Return</a></div>");}}别的{回声"<label>当前密码:</label><input type='password' id='password' name='oldpassword'><p><label>新密码:</label><input type='password' id='password' name='newpassword'><p><label>重复新密码:</label><input type='password' name='repeatnewpassword'><p><label>电子邮件:</label><input type='email' name='email'><p><input type='submit' name='submit' class='submit' value='submit'><br><br><br><h2><p><a href='index2.php'>返回</a></p></h2></表单>";}{别的die("您必须登录才能更改密码");?>

解决方案

问题是你两个查询紧接着运行,你需要添加一个检查,看看密码是否为空,然后跳过密码查询,如果电子邮件为空则跳过电子邮件查询

if ($newpassword != "") {//检查密码如果($oldpassword==$oldpassworddb){//检查两个新密码如果($newpassword==$repeatnewpassword){//成功//修改数据库中的密码$querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE username='$username'");别的die("<div class='results'>新密码不匹配!</div>");}别的die("<div class='results'>旧密码不匹配!</div>");}}如果 ($email != "") {$querychange = mysql_query("UPDATE login SET email='$email' WHERE username='$username'");die("<div class='successmate'>您的密码已更改.<a href='index2.php'><br><br> Return</a></div>");}

完整代码集(OP 要求)

Honda |<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><link href='http://fonts.googleapis.com/css?family=Julius+Sans+One' rel='stylesheet' type='text/css'><link href="../css/style.css" rel="stylesheet" type="text/css" media="all"/><?phpsession_start();$username = $_SESSION['sess_user'];echo '<div class="search1"><h2>'.$username.'</h2></div>';如果(isset($_SESSION['sess_user'])){//用户已登录if (isset($_POST['submit'])) {//开始修改密码//检查字段$oldpassword = md5($_POST['oldpassword']);$newpassword = md5($_POST['newpassword']);$email = $_POST['email'];$repeatnewpassword = md5($_POST['repeatnewpassword']);//根据数据库检查密码include('../includes/config.php');$queryget = mysql_query("SELECT password FROM login WHERE username='$username'")或死(更改密码失败");$row = mysql_fetch_assoc($queryget);$oldpassworddb = $row['password'];如果($新密码!="){//检查密码如果($oldpassword==$oldpassworddb){//检查两个新密码如果($newpassword==$repeatnewpassword){//成功//修改数据库中的密码$querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE username='$username'");die("

您的密码已被更改.<br><br>返回

");} 别的 {die("<div class='results'>新密码不匹配!</div>");}} 别的 {die("<div class='results'>旧密码不匹配!</div>");}}如果 ($email != "") {$querychange = mysql_query("UPDATE login SET email='$email' WHERE username='$username'");die("

您的电子邮件已更改.<br><br>返回

");}} 别的 {echo "";echo "<label>旧密码:</label> <input type='password' id='password' name='oldpassword'>";echo "<p><label>新密码:</label>";echo "<input type='password' id='password' name='newpassword'>";echo "<p><label>重复新密码:</label>";echo "<input type='password' name='repeatnewpassword'><p>";echo "<label>Email:</label> <input type='email' name='email'>";echo "<p><input type='submit' name='submit' class='submit' value='submit'>";echo "<br><br><br><h2><p><a href='index2.php'>返回</a></p></h2><;/表格>";}} 别的 {die("您必须登录才能更改密码");}?><img src="../images/main.jpg">

just created a 'changedetails.php' page where users are able to change their details such as 'password' and 'email'. They all work successful but I am trying to allow a user to change their email without having to change their password as well.

So basically, if a user only wanted to change their email it will update in the DB without having to change their passwords.

Code:

<title>Honda |</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href='http://fonts.googleapis.com/css?family=Julius+Sans+One' rel='stylesheet' type='text/css'>
<link href="../css/style.css" rel="stylesheet" type="text/css" media="all" />

<?php
session_start();

$username = $_SESSION['sess_user'];

echo '<div class="search1"><h2>'.$username.'</h2></div>';

if (isset($_SESSION['sess_user'])) {
    //user is logged in
    if (isset($_POST['submit'])) {
        //start changing password
        //check fields

        $oldpassword = md5($_POST['oldpassword']);
        $newpassword = md5($_POST['newpassword']);
        $email = $_POST['email'];

        $repeatnewpassword = md5($_POST['repeatnewpassword']);


        //check password against db
        include('../includes/config.php');

        $queryget = mysql_query("SELECT password 
                                 FROM login 
                                 WHERE username='$username'") 
                    or die ("change password failed");
        $row = mysql_fetch_assoc($queryget);
        $oldpassworddb = $row['password'];

        //check passwords
        if ($oldpassword==$oldpassworddb) {
            //check two new passwords
            if ($newpassword==$repeatnewpassword) {
                //successs
                //change password in db

                $querychange = mysql_query("UPDATE login 
                                            SET password='$newpassword' 
                                            WHERE username='$username'");
                $querychange = mysql_query("UPDATE login 
                                            SET email='$email' 
                                            WHERE username='$username'");
                die("<div class='successmate'>
                     Your password has been changed. <a href='index2.php'>
                     <br><br> Return</a></div>");
            } else {
                die("<div class='results'>New password doesn't match!</div>");
            }
        } else {
            die("<div class='results'>Old password doesn't match!</div>");
        }
    } else {
        echo "<form class='search1' action='changepassword.php' method='POST'>";
        echo "<label>Old Password:</label> <input type='password' id='password' name='oldpassword'>";
        echo "<p><label>New Password:</label> ";
        echo "<input type='password' id='password' name='newpassword'>";
        echo "<p><label>Repeat New Password:</label> ";
        echo "<input type='password'  name='repeatnewpassword'><p>";
        echo "<label>Email:</label> <input type='email'  name='email'>";
        echo "<p><input type='submit' name='submit' class='submit' value='submit'>";
        echo "<br><br><br><h2><p><a href='index2.php'>Back</a></p></h2></form>";
     }
 } else { 
     die ("You must be logged in to change your password");
 }

 ?>

<img src="../images/main.jpg">

Thanks!

UPDATE 2:

if ($newpassword != "")  {
  //check passwords
  if ($oldpassword==$oldpassworddb)
  {
  //check two new passwords
  if ($newpassword==$repeatnewpassword)
  {
  //successs
  //change password in db

  $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE username='$username'");
  }
  else 
die("<div class='results'>New password doesn't match!</div>");


}else 
die("<div class='results'>Old password doesn't match!</div>");

}

}


if ($email != "")  {
$querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
die("<div class='successmate'>Your password has been changed. <a href='index2.php'><br><br> Return</a></div>");
}

}
else
{

echo"
<form class='search1' action='changepassword.php' method='POST'>
<label>Current Password:</label> <input type='password' id='password' name='oldpassword'><p>
<label>New Password:</label> <input type='password' id='password' name='newpassword'><p>
<label>Repeat New Password:</label> <input type='password'  name='repeatnewpassword'><p>
<label>Email:</label> <input type='email'  name='email'><p>
<input type='submit' name='submit' class='submit' value='submit'><br><br><br>
<h2><p><a href='index2.php'>Back</a></p></h2>
</form>
";
 }

 {else 
die ("You must be logged in to change your password");


?>

解决方案

The problem is you are running the 2 queries right after each other, you need to add a check to see if password is blank, then skip password query, if email is blank then skip email query

EDIT:

if ($newpassword != "")  {
  //check passwords
  if ($oldpassword==$oldpassworddb)
  {
  //check two new passwords
  if ($newpassword==$repeatnewpassword)
  {
  //successs
  //change password in db

  $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE username='$username'");
  else 
die("<div class='results'>New password doesn't match!</div>");

}else 
die("<div class='results'>Old password doesn't match!</div>");

}

}


if ($email != "")  {
$querychange = mysql_query("UPDATE login SET email='$email' WHERE   username='$username'");
die("<div class='successmate'>Your password has been changed. <a href='index2.php'><br><br> Return</a></div>");
}

FULL CODE SET(Requested by OP)

<title>Honda |</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href='http://fonts.googleapis.com/css?family=Julius+Sans+One' rel='stylesheet' type='text/css'>
<link href="../css/style.css" rel="stylesheet" type="text/css" media="all" />

<?php
session_start();

$username = $_SESSION['sess_user'];

echo '<div class="search1"><h2>'.$username.'</h2></div>';

if (isset($_SESSION['sess_user'])) {
    //user is logged in
    if (isset($_POST['submit'])) {
        //start changing password
        //check fields

        $oldpassword = md5($_POST['oldpassword']);
        $newpassword = md5($_POST['newpassword']);
        $email = $_POST['email'];

        $repeatnewpassword = md5($_POST['repeatnewpassword']);


        //check password against db
        include('../includes/config.php');

        $queryget = mysql_query("SELECT password FROM login WHERE username='$username'") 
                or die ("change password failed");
    $row = mysql_fetch_assoc($queryget);
    $oldpassworddb = $row['password'];

    if ($newpassword != "")   {
        //check passwords
        if ($oldpassword==$oldpassworddb) {
            //check two new passwords
            if ($newpassword==$repeatnewpassword) {
               //successs
               //change password in db

               $querychange = mysql_query("UPDATE login SET password='$newpassword' WHERE username='$username'");


     die("<div class='successmate'>
                 Your password has been changed. <a href='index2.php'>
                 <br><br> Return</a></div>");
        } else {
            die("<div class='results'>New password doesn't match!</div>");
        }
    } else {
        die("<div class='results'>Old password doesn't match!</div>");
    }

    }

            if ($email != "")    {
                $querychange = mysql_query("UPDATE login SET email='$email' WHERE username='$username'");

die("<div class='successmate'>
                 Your email been changed. <a href='index2.php'>
                 <br><br> Return</a></div>");

            }
                } else {
    echo "<form class='search1' action='changepassword.php' method='POST'>";
    echo "<label>Old Password:</label> <input type='password' id='password' name='oldpassword'>";
    echo "<p><label>New Password:</label> ";
    echo "<input type='password' id='password' name='newpassword'>";
    echo "<p><label>Repeat New Password:</label> ";
    echo "<input type='password'  name='repeatnewpassword'><p>";
    echo "<label>Email:</label> <input type='email'  name='email'>";
    echo "<p><input type='submit' name='submit' class='submit' value='submit'>";
    echo "<br><br><br><h2><p><a href='index2.php'>Back</a></p></h2></form>";
 }
 } else { 
     die ("You must be logged in to change your password");
 }

 ?>

<img src="../images/main.jpg">

这篇关于相同的形式,但有输入做其他事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆