我有两个不同的按钮。一个用于删除用户,另一个用于更改电子邮件地址。问题是单击change email按钮实际上会将用户从数据库中删除。
header.php
<?php
session_start();
$cookie_name = "LoginSystem";
$cookie_value = "Valid";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css">
<meta charset="UTF-8">
<meta name="description" content="Enrol site for activites">
<meta name="keywords" content="enrol, activities, school, hobby, college, login, register">
<meta name="author" content="Gyorgy Hadhazy">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header>
<nav>
<div class="main-wrapper">
<ul>
<li><a href="index.php">HOME</a></li>
<li><a href="about.php">ABOUT</a></li>
<li><a href="media.php">MEDIA</a></li>
<li><a href="activities.php">ACTIVITIES</a></li>
<li><a href="contact.php">CONTACT</a></li>
</ul>
<div class="nav-login">
<?php
if (isset($_SESSION['u_id'])) {
echo '
<form action="includes/logout.inc.php" method="POST">
<button type="submit" name="submit">Logout</button>
</form>
';
echo '<form action="deleteusr.php" method="POST">
<button type="submit" name="delete">Delete User</button>
<input type="hidden" name="user_uid" value="'. $_SESSION['u_id'].'"
</form>';
} else{
echo '
<form action="includes/login.inc.php" method="POST">
<input type="text" name="uid" placeholder="StudentID/email">
<input type="password" name="pwd" placeholder="password">
<button type="submit" name="submit">LOGIN</button>
</form>
<a href="signup.php">SIGN UP</a>
';
}
?>
<button type="button" onclick="resizeText(1)" name="resizeplus" class="resize-plus">+ Text size</button>
<button type="button" onclick="resizeText(-1)" name="resizenegative">- Text size</button>
<script>
function resizeText(multiplier) {
if (document.body.style.fontSize == "") {
document.body.style.fontSize = "1.0em";
}
document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.2) + "em";
}
</script>
</div>
</div>
</nav>
</header>
index.php
<?php
include 'header.php';
?>
<style>
header{
text-align: center;
}
body{
text-align: center;
}
</style>
<section class="main-container">
<div class="main-wrapper">
<h2>HOME</h2>
<p>Please log in if extra features are not displayed</p>
<?php
if (isset($_SESSION['u_email'])) {
echo '<form action="changeEmail.php" method="POST">
<button type="submit" name="email">Change Email</button>
<input type="text" name="email" value="'. $_SESSION['u_email'].'"
</form>'; }
?>
</div>
</section>
<?php
include 'footer.php';
?>
最后是它应该调用的php文件:changeEmail.php
<?php
include 'header.php';
?>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "loginsystem";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$email = $_SESSION['u_ email'];
$sql = "UPDATE users SET user_email='$email'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
我认为问题出在header.php中,但我不是很确定。如果有人能帮我指出这个问题,我将不胜感激。
由index.php呈现的HTML代码
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css">
<meta charset="UTF-8">
<meta name="description" content="Enrol site for activites">
<meta name="keywords" content="enrol, activities, school, hobby, college, login, register">
<meta name="author" content="Gyorgy Hadhazy">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header>
<nav>
<div class="main-wrapper">
<ul>
<li><a href="index.php">HOME</a></li>
<li><a href="about.php">ABOUT</a></li>
<li><a href="media.php">MEDIA</a></li>
<li><a href="activities.php">ACTIVITIES</a></li>
<li><a href="contact.php">CONTACT</a></li>
</ul>
<div class="nav-login">
<form action="includes/logout.inc.php" method="POST">
<button type="submit" name="submit">Logout</button>
</form>
<form action="deleteusr.php" method="POST">
<button type="submit" name="delete">Delete User</button>
<input type="hidden" name="user_uid" value="6"
</form>
<button type="button" onclick="resizeText(1)" name="resizeplus" class="resize-plus">+ Text size</button>
<button type="button" onclick="resizeText(-1)" name="resizenegative">- Text size</button>
<script>
function resizeText(multiplier) {
if (document.body.style.fontSize == "") {
document.body.style.fontSize = "1.0em";
}
document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.2) + "em";
}
</script>
</div>
</div>
</nav>
</header>
<style>
header{
text-align: center;
}
body{
text-align: center;
}
</style>
<section class="main-container">
<div class="main-wrapper">
<h2>HOME</h2>
<p>Please log in if extra features are not displayed</p>
<form action="changeEmail.php" method="POST">
<button type="submit" name="email">Change Email</button>
<input type="text" name="email" value="test11@gmail.com"
</form>
</div>
</section>
Cookie 'LoginSystem' is set!<br>Value: Valid
实际外观的图像:enter image description here
发布于 2018-06-09 03:46:55
主要问题:
有两个缺少结束>
字符的<input>
标记。这意味着浏览器正在构造一个不准确的DOM树。它正在尽最大努力确定您要提交的表单,但它选择了错误的表单(删除表单)。
第一个示例是用header.php编写的:
<input type="hidden" name="user_uid" value="'. $_SESSION['u_id'].'"
请注意,没有结束该输入标记的>
。
然后在index.php中:
<input type="text" name="email" value="'. $_SESSION['u_email'].'"
将结束的>
字符添加到这两个字符中,当您单击按钮时,浏览器将很高兴地解析DOM并选择要提交的正确表单。
其他问题:
ChangeEmail.php中有几个问题:
$email = $_SESSION['u_ email'];
需要的是
$email = $_SESSION['u_email'];
否则,$email
将始终是一个空字符串(或者您不想要的其他值--我不确定$_SESSIONS
的行为),并且您将把所有电子邮件设置为空字符串。
第二个问题是您的SQL:
$sql = "UPDATE users SET user_email='$email'";
您需要使用where
子句指定要设置的用户的电子邮件。否则,您会将每个电子邮件都设置为$email
的值。
在这种情况下,您需要从发布的表单数据中获取新的电子邮件地址。
$new_email = $_POST["email"];
$sql = "UPDATE users SET user_email='$new_email' WHERE user_email='$email'";
要确保获得新的email
表单数据,请从button
元素中删除name
属性-这不是必需的。
https://stackoverflow.com/questions/50767323
复制相似问题