gpt4 book ai didi

php - 相同的形式但有输入做其他事情

转载 作者:行者123 更新时间:2023-11-29 23:32:57 25 4
gpt4 key购买 nike

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

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

代码:

<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">

谢谢!

更新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");


?>

最佳答案

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

编辑:

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>");
}

完整代码集(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">

关于php - 相同的形式但有输入做其他事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26481182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com