我有两个不同的按钮。一个用于删除用户,另一个用于更改电子邮件地址。问题是单击更改电子邮件按钮实际上会从数据库中删除用户。
的header.php
$event = new Event();
$event->foo = "foo";
$event->bar = "bar";
$event->save();
dd($event);
的index.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>
最后应该调用的php文件: changeEmail.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';
?>
我认为问题出在header.php中,但我不确定。如果有人愿意指出这个问题,我会非常感激。
index.php呈现的HTML代码
<?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();
?>
实际外观的图像: enter image description here
答案 0 :(得分:1)
主要问题:
有两个<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
表单数据,请从name
元素中删除button
属性 - 这不是必需的。