Hi,

I am new to php development! I have email table in my database which has email records. On the page read_emails.php, i have two buttons, one is of Reply and second is of Delete. My delete button is not deleting the email record in the database. Please help me where i am wrong and what error i am doing in developing the delete button. The code goes as below!

require_once("admin.inc.php");
require_once("../db.php");

		$id = $_GET['id'];
		$q = "SELECT * FROM clf_email WHERE emailID = '$id'";
		$result = mysql_query($q);

		
		if(isset($_POST['Delete'])){
		$sql = "DELETE FROM clf_email WHERE emailID = '$id'";
		$result = mysql_query($sql) or mysql_error();;
		$msg = 'deleted!';
		}
}
?>
<form action="" method="post">
<table width="557" height="135" border="0" class="email_tbl">

<?php if(mysql_num_rows($result) < 1){
 echo "<div align='center' class='err'><b>There is no data available in the database!</b></div>";
 }else {

 while( $row = mysql_fetch_array($result)){
?>
 <tr>
    <td colspan="5" id="email_txt">Date: <?php echo $row['date']; ?></td>
    </tr>
  <tr>

 <td width="152" id="email_txt">Sender's email:</td>
    <td colspan="4" id="email"><?php echo $row['email']; ?></td>
  </tr>

  <tr>
    <td width="152" id="email_txt">Subject:</td>
    <td colspan="4" id="email"><?php echo $row['subject']; ?></td>
  </tr>
  <tr>

    <td id="email_txt">Message:</td>
    <td colspan="4" rowspan="2" valign="top" id="email_txt"><?php echo $row['message']; ?></td>
  </tr>
  <tr>
 <td>&nbsp;</td>
    </tr>
    
  <tr>
   
    <?php if (!$replied){ ?>

    <td>&nbsp;</td>

    <td width="152"><a href="reply_mail.php?id=<?php echo $row['emailID'];?>" >
      <input type="submit" name="reply" value="Reply" class="button"></a></td>

    <td width="235"><a href="read_emails.php?id=<?php echo $row['emailID'];?>" >
      <input type="submit" name="Delete" value="Delete" class="cautionbutton" onClick="return(confirm('Are you sure you want to delete this email?));">delete</a></td>

    <?php }else{  ?>
    <!--<td width="108" id="replied_btn">Replied</td>-->
    <?php }; ?>
  </tr>
  
    <?php } }; ?>
</table>

Recommended Answers

All 13 Replies

Hi,

- You are not sending any value with the button (ID). $_GET recieves the values passed along in the links - The DELETE button is sending a $_POST request.

I would send the unique id of the email that you want to delete, via a HIDDEN FIELD.

<input type="hidden" name="delete" value="<?php echo $row['emailID'];?>" />

And then recieve it like this:

// U are getting the id via a $_POST request..
if(isset($_POST['delete'])){
$id = mysqli_real_escape_string($connection, $_POST['delete']);

// Use the $id to delete from the DB
$query = "DELETE FROM clf_email WHERE emailID = '$id'";
}
<input type="hidden" name="delete" value="<?php echo $row['emailID'];?>" />

// U are getting the id via a $_POST request..
$id = mysqli_real_escape_string($connection, $_POST['delete']); 

// Use the $id to delete from the DB
$query = "DELETE FROM clf_email WHERE emailID = '$id'";

<a href="reply_mail.php?id=<?php echo $row;?>" >
<input type="submit" name="reply" value="Reply" class="button"></a>

What for the submit button inside anchor ? Put 'reply_main.php' for action of the form and all pass via form with POST method is better.

Hi Klemme,

Loads of thanks for your help and time!

Well, i have tried the things you mentions in your solution but all in vein! I think i am doing the things in wrong way. I have taken the hidden input as mention in the solution and changed the code as well to get the desired results but nothing happened! The changed code is given below, please check it out and give your suggestion!

/*$id = $_GET['id'];*/
		$q = "SELECT * FROM clf_email WHERE emailID = '$id'";
		$result = mysql_query($q);

		
		if(isset($_POST['Delete'])){
		
		$id = mysqli_real_escape_string($connection, $_POST['delete']);
		$sql = "DELETE FROM clf_email WHERE emailID = '$id'";
		$result = mysql_query($sql) or mysql_error();;
		$msg = 'deleted!';
		}

Hi,

I would suggest that you keep the two files separate, to get a better view of things.
1) page with youe email or other things on it.
2) Create a new file = process_delete.php

You need to be able to send the unique id to the processing script. This way you can make a query to the database and target the right record to be deleted.

In your script you have put the submit button inside a link, that wont work im sure?

If you have a link that passes along the id for that specific email, get the id and use it at the bottom of your form in a hidden field.

// Tjeck your link for the id on the page that is NOT the processing page!
if(isset($_GET['id'])){
$id = mysql_real_escape_string($connection, $_GET['id']);
//$connection should be YOUR DATABASE CONNECTION NAME :-) 
}
// Now you should have the id that is unique for that specific email you want
// delete from your DB. It is taken from the link, and put insde $id
// Next you have your form where you send/submit that $id, so you can recieve on 
// process_delete.php
<form action="process_delete.php" name="form" id="form" />
<input type="hidden" name="delete_email" id="delete_email" value="<?php echo $id; ?>"
</form>

On the process_delete.php page you recieve the $id.

// Tjeck if the form has been submitted
if(isset(delete_email)){
// Get the id from the hidden field
$id = mysql_real_escape_string($connection, $_POST['delete_email']);
}
// $connection is you db connection, so insert you db con name there.

Then you can query the DB and delete the record.

$query = mysql_query("DELETE FROM clf_email WHERE emailID = '$id'") or die (mysql_error($connection)); // $again your own db conneection..
if($query){
echo 'Your record with id number: ' . $id . ' has been deleted!';
echo '<a href="your_page.php">Go back to your inbox</a>';
}

emailID, needs to be your unique id, the primary key from the clf_email.

Try this and see if it works.

I normally use mysqli, and not mysql - I am not sure of you need to write the:
$connection, first or last, or at all in the query, but that is not a big effort to play around with.

Jan

How do you delete Emails? Do you list Emails with link to delete or one fills address and the form submission does delete?
If you list Emails, add Id to delete link. If you use form, that is easy. Since email is unique it can be used instead of ID!

just replace this line

<td width="235"><a href="read_emails.php?id=<?php echo $row['emailID'];?>" >
<input type="submit" name="Delete" value="Delete" class="cautionbutton" onClick="return(confirm('Are you sure you want to delete this email?));">delete</a></td>

with

<td width="235"><a href="javascript:confirmdel(<?php echo $row['emailID'];?>);" >Delete</a></td>

javascript is,

function confirmdel(delid)
		{
			var check = confirm("Are you sure to delete this email id?");
			if(check==1)
			{
				window.location="yourFileName.php?delid="+delid;
			}
		}

and the php is

if(isset($_GET['delid']))
	{
		$sql = "DELETE FROM clf_email WHERE emailID = '".$_GET['delid']."'";
$result = mysql_query($sql) or mysql_error();;
$msg = 'deleted!';
	}

Thanks Karthik for your precious suggestion!

But i am confused about one thing and that is about my way of getting record id by using the $_GET array. I always save the $_GET array in a variable called $id and i do not know i always get fail in getting the record id. You did the same thing by using the $_GET array but did not save it in any variable and it is working quite well! What logical error i commit in getting a record id? Will you, please, enlighten me on this!

Thanks in Advance!
Best Regards!

<td width="235"><a href="read_emails.php?id=<?php echo $row['emailID'];?>" >
<input type="submit" name="Delete" value="Delete" class="cautionbutton" onClick="return(confirm('Are you sure you want to delete this email?));">delete</a></td>

You are using link in the submit button. That is the problem. Use image instead of submit button.

Just a small note:

If your learning PHP, I think it would be better to try and finish the delete query, make it work, just using php. As it is an approach, you will benefit from, a great deal in the future, when developing php applications. Its definately not a unique query/problem you have.

I would suggest to stay away from javascript/clientside, and try to deal with it with php/serverside, as your initial approach did.

It is just a thought, and I have had the same questions a while ago, but now i am happy that i sticked with it, and made it work, as mentioned above, if you will keep learning php you have to learn this bit at least.

Just a little motivator :-)

Sort out how you get the id from the $_GET in the link, when an email is viewed. Getting that id, you can delete the email, using the approach i wrote above, by sending the id in a hidden field, getting it via php in the processing script, and do the delete query.

Klemme

ok! got it!
Lots of thanks for the help, Karthik!

Thanks to Klemme as well for his suggestions!:)

Hi,

I am new to php development! I have email table in my database which has email records. On the page read_emails.php, i have two buttons, one is of Reply and second is of Delete. My delete button is not deleting the email record in the database. Please help me where i am wrong and what error i am doing in developing the delete button. The code goes as below!

require_once("admin.inc.php");
require_once("../db.php");

		$id = $_GET['id'];
		$q = "SELECT * FROM clf_email WHERE emailID = '$id'";
		$result = mysql_query($q);

		
		if(isset($_POST['Delete'])){
		$sql = "DELETE FROM clf_email WHERE emailID = '$id'";
		$result = mysql_query($sql) or mysql_error();;
		$msg = 'deleted!';
		}
}
?>
<form action="" method="post">
<table width="557" height="135" border="0" class="email_tbl">

<?php if(mysql_num_rows($result) < 1){
 echo "<div align='center' class='err'><b>There is no data available in the database!</b></div>";
 }else {

 while( $row = mysql_fetch_array($result)){
?>
 <tr>
    <td colspan="5" id="email_txt">Date: <?php echo $row['date']; ?></td>
    </tr>
  <tr>

 <td width="152" id="email_txt">Sender's email:</td>
    <td colspan="4" id="email"><?php echo $row['email']; ?></td>
  </tr>

  <tr>
    <td width="152" id="email_txt">Subject:</td>
    <td colspan="4" id="email"><?php echo $row['subject']; ?></td>
  </tr>
  <tr>

    <td id="email_txt">Message:</td>
    <td colspan="4" rowspan="2" valign="top" id="email_txt"><?php echo $row['message']; ?></td>
  </tr>
  <tr>
 <td>&nbsp;</td>
    </tr>
    
  <tr>
   
    <?php if (!$replied){ ?>

    <td>&nbsp;</td>

    <td width="152"><a href="reply_mail.php?id=<?php echo $row['emailID'];?>" >
      <input type="submit" name="reply" value="Reply" class="button"></a></td>

    <td width="235"><a href="read_emails.php?id=<?php echo $row['emailID'];?>" >
      <input type="submit" name="Delete" value="Delete" class="cautionbutton" onClick="return(confirm('Are you sure you want to delete this email?));">delete</a></td>

    <?php }else{  ?>
    <!--<td width="108" id="replied_btn">Replied</td>-->
    <?php }; ?>
  </tr>
  
    <?php } }; ?>
</table>

I use the following two codes

This is where they confirm deletion:

<?php
$email = $_GET['email'];
?>
<html>
<head>
  <title>Remove Email</title>
</head>
<body>
  <p>Enter an email address to remove</p>
  <form method="post" action="removeemail.php">
    <label for="email">Email address:</label><br />
    <input id="email" name="email" value="<?php echo $email; ?>" type="text" size="30" /><br />
    <input type="submit" name="Remove" value="Unsubscribe" />
  </form>
</body>
</html>

(The GET function allows you to have an email in the url) (e.g. my-site.com/remove.php?email=me@example.com. Then, me@example.com goes into the text box.)

This code actually removes the email (removeemail.php)

<?php
  $dbc = mysqli_connect('server', 'username', 'password', 'database')
    or die('Error connecting to MySQL server.');

  $email = $_POST['email'];

  $query = "DELETE FROM table WHERE email = '$email'";
  mysqli_query($dbc, $query)
    or die('Error querying database.');

  echo 'Email removed: ' . $email;

  mysqli_close($dbc);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.