我正在编写一个 php 和 html 程序,它将执行以下电子邮件管理功能: 添加电子邮件,将用户的名字、姓氏和电子邮件地址信息添加到 MariaDB,发送电子邮件,简单地向数据库中的所有用户发送电子邮件,以及删除电子邮件 这将从数据库中删除有关用户的名字、姓氏和电子邮件地址的信息。 问题是我的“删除电子邮件”选项不起作用,而是显示以下错误:
我想要实现的是能够选择要删除的电子邮件帐户,例如:
非常感谢您的帮助,这是我的 index.php 文件:
<?php
// Establish connection to MySQL
$dbc = mysqli_connect('localhost', 'root', '', 'email_list') or die('Error connecting to MySQL server.');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['add_email'])) {
// Add Email to Database
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$query = "INSERT INTO customers (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')";
if (mysqli_query($dbc, $query)) {
echo "<div class='alert alert-success' role='alert'>Data added to the database successfully!</div>";
} else {
echo "<div class='alert alert-danger' role='alert'>Error querying database: " . mysqli_error($dbc) . "</div>";
}
} elseif (isset($_POST['send_email'])) {
// Send Email to DB Users
$subject = $_POST['subject'];
$text = $_POST['emailtext'];
if (empty($subject) && empty($text)) {
echo '<div class="alert alert-danger" role="alert">You forgot the email subject and body text.</div>';
} elseif (empty($subject)) {
echo '<div class="alert alert-danger" role="alert">You forgot the email subject.</div>';
} elseif (empty($text)) {
echo '<div class="alert alert-danger" role="alert">You forgot the email body text.</div>';
} else {
// Code to send the email would be placed here
echo '<div class="alert alert-success" role="alert">Email sent successfully.</div>';
}
} elseif (isset($_POST['remove_email'])) {
// Remove Email from Database
if (isset($_POST['todelete']) && is_array($_POST['todelete'])) {
foreach ($_POST['todelete'] as $delete_id) {
$query = "DELETE FROM customers WHERE id = $delete_id";
mysqli_query($dbc, $query) or die('Error querying database.');
}
echo '<div class="alert alert-success" role="alert">Customer(s) removed.</div>';
} else {
echo '<div class="alert alert-danger" role="alert">No customers selected for removal.</div>';
}
}
}
// Close MySQL connection
mysqli_close($dbc);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Management</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="mt-4">Email Management</h1>
<!-- Add Email Form -->
<div class="mt-5">
<h2>Add Email</h2>
<form method="post">
<div class="form-group">
<label for="first_name">First Name:</label>
<input type="text" class="form-control" name="first_name" id="first_name" required>
</div>
<div class="form-group">
<label for="last_name">Last Name:</label>
<input type="text" class="form-control" name="last_name" id="last_name" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="email" id="email" required>
</div>
<button type="submit" class="btn btn-primary" name="add_email">Add Email</button>
</form>
</div>
<!-- Send Email Form -->
<div class="mt-5">
<h2>Send Email</h2>
<form method="post">
<div class="form-group">
<label for="subject">Subject of email:</label>
<input id="subject" name="subject" type="text" class="form-control" placeholder="Enter email subject" />
</div>
<div class="form-group">
<label for="emailtext">Body of email:</label>
<textarea id="emailtext" name="emailtext" class="form-control" rows="8" placeholder="Enter email body text"></textarea>
</div>
<button type="submit" name="send_email" class="btn btn-primary">Send Email</button>
</form>
</div>
<!-- Remove Email Form -->
<div class="mt-5">
<h2>Remove Email</h2>
<form method="post">
<?php
$query = "SELECT * FROM customers";
$result = mysqli_query($dbc, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo '<div class="form-check">';
echo '<input class="form-check-input" type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
echo '<label class="form-check-label">' . $row['first_name'] . ' ' . $row['last_name'] . ' - ' . $row['email'] . '</label>';
echo '</div>';
}
echo '<button type="submit" name="remove_email" class="btn btn-danger mt-3">Remove Selected Emails</button>';
} else {
echo '<div class="alert alert-info" role="alert">No emails to display.</div>';
}
?>
</form>
</div>
<a href="#" class="btn btn-primary mt-4">Back to Main</a>
</div>
<!-- Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
还有我的和index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Management</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="mt-4">Email Management</h1>
<!-- Add Email Form -->
<div class="mt-5">
<h2>Add Email</h2>
<form method="post">
<div class="form-group">
<label for="first_name">First Name:</label>
<input type="text" class="form-control" name="first_name" id="first_name" required>
</div>
<div class="form-group">
<label for="last_name">Last Name:</label>
<input type="text" class="form-control" name="last_name" id="last_name" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="email" id="email" required>
</div>
<button type="submit" class="btn btn-primary" name="add_email">Add Email</button>
</form>
</div>
<!-- Send Email Form -->
<div class="mt-5">
<h2>Send Email</h2>
<form method="post">
<div class="form-group">
<label for="subject">Subject of email:</label>
<input id="subject" name="subject" type="text" class="form-control" placeholder="Enter email subject" />
</div>
<div class="form-group">
<label for="emailtext">Body of email:</label>
<textarea id="emailtext" name="emailtext" class="form-control" rows="8" placeholder="Enter email body text"></textarea>
</div>
<button type="submit" name="send_email" class="btn btn-primary">Send Email</button>
</form>
</div>
<!-- Remove Email Form -->
<div class="mt-5">
<h2>Remove Email</h2>
<form method="post">
<?php
$query = "SELECT * FROM customers";
$result = mysqli_query($dbc, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo '<div class="form-check">';
echo '<input class="form-check-input" type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
echo '<label class="form-check-label">' . $row['first_name'] . ' ' . $row['last_name'] . ' - ' . $row['email'] . '</label>';
echo '</div>';
}
echo '<button type="submit" name="remove_email" class="btn btn-danger mt-3">Remove Selected Emails</button>';
} else {
echo '<div class="alert alert-info" role="alert">No emails to display.</div>';
}
?>
</form>
</div>
<a href="#" class="btn btn-primary mt-4">Back to Main</a>
</div>
<!-- Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
您的代码发送一个 POST 请求,并且该请求正在内部处理
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//...
}
部分。然后,关闭连接:
// Close MySQL connection
mysqli_close($dbc);
之后,您尝试在此已关闭的连接上执行查询:
$query = "SELECT * FROM customers";
$result = mysqli_query($dbc, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo '<div class="form-check">';
echo '<input class="form-check-input" type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
echo '<label class="form-check-label">' . $row['first_name'] . ' ' . $row['last_name'] . ' - ' . $row['email'] . '</label>';
echo '</div>';
}
echo '<button type="submit" name="remove_email" class="btn btn-danger mt-3">Remove Selected Emails</button>';
} else {
echo '<div class="alert alert-info" role="alert">No emails to display.</div>';
}
结果你会得到你提到的错误。解决方案:
如果关闭连接,请在运行完依赖该连接的任何查询后执行此操作:因此,您可以删除连接关闭,或者将其移到最后一个查询之后,或者在关闭连接之前和稍后移动最后一个查询处理结果而不是查询结果。