在PHP中连续删除记录 [英] DELETE record in a row in PHP
问题描述
我试图删除我的数据库中的记录。所以基本上我创建了一个包含我所有记录的表格。现在我需要做的是当我点击DELETE链接时,它会删除记录选定的行。
I was trying to delete a record on my Database. So basically I created a table that contains all of my records. Now what I need to do is when I click on the "DELETE" link it would delete the record selected row.
下面是它的样子:
所以基本上我在这里有3页。
1. page.php
2. add.php
3. delete.php
So basically I have 3 pages here.
1. page.php
2. add.php
3. delete.php
这是我的page.php文件:
Here's my page.php file:
<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文件:
Here's my add.php file:
<?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文件:
Here's my delete.php file so far:
<?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');
?>
现在,当我单击删除链接时,它必须实时删除按钮。任何想法?
So now when I click the delete link it must delete the button real time. Any idea?
推荐答案
修改您的delete.php以检索url参数:
Modify your delete.php to retrieve the url parameter:
<?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,你正在使用这个:
As for your add.php, you are using this:
$insertEmail = run_mysql_query($query);
if(run_mysql_query($query))
您应该将其更改为这个:
You should change it to this:
$insertEmail = run_mysql_query($query);
if($insertEmail)
你在做什么现在你通过调用run_mysql_query两次来执行查询两次。这应该解决它
What you are doing right now is you are executing the query twice by calling run_mysql_query twice. This should fix it
这篇关于在PHP中连续删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!