Reputation:
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.
Here's what it looks like:
So basically I have 3 pages here.
1. page.php
2. add.php
3. delete.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>
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');
?>
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?
Upvotes: 5
Views: 1426
Reputation: 21661
Sense when what run_mysql_query
a function in php?
http://php.net/manual-lookup.php?pattern=run_mysql_query&scope=quickref
Upvotes: 1
Reputation: 703
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');
?>
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)
What you are doing right now is you are executing the query twice by calling run_mysql_query twice. This should fix it
Upvotes: 4
Reputation: 192
$query = "DELETE FROM email_tbl WHERE id=".$_GET['id']." LIMIT 1";
Upvotes: 0
Reputation: 763
Update the delete.php file:
$id = $_GET['id'];
$query = "DELETE FROM email_tbl WHERE id='$id' LIMIT 1";
and also check the section below: You are running the query twice. so it is obvious it will add the same record twice.
$insertEmail = run_mysql_query($query);
if(run_mysql_query($query))
{
$_SESSION['message'] .= "New RECORD has been added correctly!";
}
Modify you code to use the run_mysql_query once only.
Upvotes: 0