PHP 两个不同的按钮发送相同的数据
PHP Two different buttons send same data
我有两个不同的按钮。一个用于删除用户,另一个用于更改电子邮件地址。问题是点击更改电子邮件按钮实际上会从数据库中删除用户。
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 但我不太确定。如果有人能帮助指出这个问题,我将不胜感激。
HTML 由 index.php
呈现的代码
<!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
主要问题:
有两个 <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
属性 - 没有必要。
我有两个不同的按钮。一个用于删除用户,另一个用于更改电子邮件地址。问题是点击更改电子邮件按钮实际上会从数据库中删除用户。
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 但我不太确定。如果有人能帮助指出这个问题,我将不胜感激。
HTML 由 index.php
呈现的代码<!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
主要问题:
有两个 <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
属性 - 没有必要。