gpt4 book ai didi

php - 在 PHP 中连续删除记录

转载 作者:太空狗 更新时间:2023-10-29 15:49:18 24 4
gpt4 key购买 nike

我试图删除数据库中的一条记录。所以基本上我创建了一个包含我所有记录的表。现在我需要做的是当我点击“DELETE”链接时它会删除记录选择的行。

这是它的样子:

基本上我这里有 3 页。
1. 页面.php
2. 添加.php
3. 删除.php

这是我的 page.php 文件:

<table border="1">
<thead>
<th>email</th>
<th>date</th>
<th>delete</th>
</thead>

<tbody>
<tr>
<?php
foreach($emails as $mail){ ?>
<td><?php echo $mail['email']; ?></td>
<td><?php echo $mail['date']; ?></td>
<td><?php echo "<a href='delete.php?id=". $mail['id']. "'>DELETE</a>"; ?></td>
</tr>
<?php } ?>


</tbody>
</table>

这是我的 add.php 文件:

<?php
require("new-connection.php");

session_start();

$email = $_POST['email'];
if(empty($_POST['email']) AND (filter_var($email, FILTER_VALIDATE_EMAIL) === false))
{
$_SESSION['message'] = "email cannot be blank";
}else{
$query = "INSERT INTO email_tbl (email, date)
VALUES('$email', NOW())";

$insertEmail = run_mysql_query($query);


if(run_mysql_query($query))
{
$_SESSION['message'] .= "New RECORD has been added correctly!";
}
else
{
$_SESSION['message'] .= "Failed to add new Interest";
}

}


header('Location: email.php');




?>

到目前为止,这是我的 delete.php 文件:

<?php
require("new-connection.php");

session_start();


$query = "DELETE FROM email_tbl
WHERE id={id} LIMIT 1";

$deleteEmail = run_mysql_query($query);


if(run_mysql_query($query))
{
$_SESSION['message'] .= "RECORD has been DELETED correctly!";
}
else
{
$_SESSION['message'] .= "Failed to DELETE RECORD";
}



header('Location: email.php');




?>

所以现在当我点击删除链接时,它必须实时删除按钮。任何想法?

最佳答案

修改您的 delete.php 以检索 url 参数:

<?php
require("new-connection.php");
session_start();

$id = $_GET['id'];

$query = "DELETE FROM email_tbl
WHERE id='$id' LIMIT 1";

$deleteEmail = run_mysql_query($query);


if($deleteEmail)
{
$_SESSION['message'] .= "RECORD has been DELETED correctly!";
}
else
{
$_SESSION['message'] .= "Failed to DELETE RECORD";
}

header('Location: email.php');
?>

至于你的add.php,你正在使用这个:

$insertEmail = run_mysql_query($query);

if(run_mysql_query($query))

你应该把它改成这样:

$insertEmail = run_mysql_query($query);


if($insertEmail)

您现在正在做的是通过两次调用 run_mysql_query 来执行查询两次。这应该可以解决它

关于php - 在 PHP 中连续删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31284554/

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