Delete emails php... solved, thanks robert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Delete emails php... solved, thanks robert

Post by scarface222 »

Hey guys I just have a quick question. I want a messaging option on my site so I am working on the user's inbox. I used php to display the emails based on a mysql query but I also included a form so I can execute a mysql delete query to accomplish the ability to delete mail on the same page based on filled in checkboxes. I am just a little confused on how to do this in terms of relating the check-boxes to my isset function. This is my script. Any tips appreciated. Thank you in advance for your time.

Code: Select all

 
//here I just select the messages and their corresponding information
echo '<form method=\"POST\" ><input type="submit" value="Delete">';
$username=$session->username;
$messages="SELECT * FROM messages WHERE username='$username'";
$query=mysql_query($messages);
while ($row = mysql_fetch_assoc($query)) {
    $from=$row['from'];
 $subject=$row['subject'];
 $date=$row['date'];
 $message=$row['message'];
//here I display the messages and a form but I do not know how to relate the form I attempted a variable relation but I can't echo that
 echo "<tr><td><input name=\"$message\" type=\"checkbox\"></td><td>$from</td> <td><a id=\"openbox\">$subject</a></td> <td>$date</td></tr>";
 echo "<div id=\"message\">$message</div>";
 echo'</form>';
 //I know this part is wrong
 if(isset($_POST['$message'])){
                 $qryusersdelete="Delete FROM messages WHERE message='$message'";
    $rslusersdelete=mysql_query($qryusersdelete);
                 }
}
Last edited by scarface222 on Mon Sep 07, 2009 1:23 pm, edited 1 time in total.
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by Robert07 »

I would suggest using an autoincrement primary key field called emailid or something in your table, and attaching that id value to each checkbox. That way you could be sure you delete the right email. But since you are using check boxes you'll need to put the emailid in the name of each box instead of the value, because the value will be on if it's checked (in the POST array), no matter what value you give it. So you can use name=\"msg".$emailid."\" and then check for any posted value that starts with msg and pull the id off the end of it.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by scarface222 »

Hey thanks alot for the advice however when I used

Code: Select all

echo '<form action="messages.php" method=\"POST\" ><input type="submit" value="Delete">';
$username=$session->username;
$messages="SELECT * FROM messages WHERE username='$username'";
$query=mysql_query($messages);
while ($row = mysql_fetch_assoc($query)) {
    $from=$row['from'];
 $subject=$row['subject'];
 $date=$row['date'];
 $message=$row['message'];
 $emailid=$row['id'];
 echo "<tr><td><input name=\"msg".$emailid."\" type=\"checkbox\"></td><td>$from</td> <td><a id=\"openbox\">$subject</a></td> <td>$date</td></tr>";
 echo "<div id=\"message\">$message</div>";
 echo'</form>';
 
 if(isset($_POST['msg'.$emailid])){
                 $qryusersdelete="Delete FROM messages WHERE id='$emailid'";
    $rslusersdelete=mysql_query($qryusersdelete);
                 }
The form is unresponsive even when i tried changing the name of the input checkbox to 2 and the isset $POST to 2 so they are the same name it does not work. I just see msg3=on in the address the way it is right now. I have tried to find a solution but am having difficulty because I have not worked with checkboxes before.
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by Robert07 »

A good way to see what is happening with the checkboxes is to add these two lines to the top of your code:

echo "POST: <BR>";
print_r($_POST);

That way you can select different boxes and see what is being passed without checking blindly for a specific value you hope is there. For example, if you check a box with the name of msg3, then you will see a parameter passed in the post array called msg3 with a value of on. If that same box is not checked, the msg3 parameter will not be present in the post array. I hope that helps clear things up for you a bit.
Regards,
Robert
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by scarface222 »

thanks again robert for your response. I pasted those lines at the top of the code like you said and I just see the lines POST and array(). When I check a box nothing happens. What could be causing all of this do you think? I am positive that the variable $emailid is the one I want because I echoed it. It is a problem with the isset function but I am not sure why because I have not used checkboxes very often.
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by Robert07 »

It sounds to me like your form is using the GET method instead of the POST method, even though you are defining the method as POST. You mentioned that you saw msg3=on in the address, do you mean the URL? You could try printing $_REQUEST instead of $_POST to see if that is the case (or both if you want). If you check a box and you see the parameter in the $_REQUEST array you could check that array instead of the $_POST array.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by scarface222 »

Yes sorry I did mean the URL. I printed the request and got this in response however it does not change when i check or uncheck my one sample box in my sample message but the id of the message is msg3 so that is correct. How is it possible that I am using GET if I defined the form action as POST?

Response
Array ( [msg3] => on [PHPSESSID] => 9gskhjri4o7c6cgkhs697lie55 )
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by Robert07 »

Hello again,
As far as why the intended post data is being sent in the url, that is probably caused by something else on the server (possibly funky .htaccess rules are inplace, or the form is included or called from some kind of wrapper file which displays the $_POST values in the url). If you actually remove the msg3=on from the url and press return (to change the current url), then try unchecking the box and submitting it, does the msg3=on return? I suspect it would not in this case. But if you leave it in the url and then try unchecking it and submitting again, you may see the parameter in the url because it was carried over from before (not because it is being passed from the form).
Regards,
Robert
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by scarface222 »

The code is within the page in use and is the only code on the page other than a session information include which has not conflicted with any other forms I have used. The msg3=on only occurs in the url when I check the box and click submit after the box becomes unchecked. When I submit again with the box unchecked it does disappear. This is the exact code of all the php on my page. PS I appreciate you taking the time to help me with this issue. I am rather new to web design and any help is appreciated.

Code: Select all

if(!$session->logged_in){
 
   header("Location: index.php");
       
}
else{
    echo "POST: <BR>";
print_r($_REQUEST);
    echo '<form action="messages.php" method=\"POST\" ><input type="submit" value="Delete">';
$username=$session->username;
$messages="SELECT * FROM messages WHERE username='$username'";
$query=mysql_query($messages);
while ($row = mysql_fetch_assoc($query)) {
    $from=$row['from'];
 $subject=$row['subject'];
 $date=$row['date'];
 $message=$row['message'];
 $emailid=$row['id'];
 echo "<tr><td><input name=\"msg".$emailid."\" type=\"checkbox\"></td><td>$from</td> <td><a id=\"openbox\">$subject</a></td> <td>$date</td></tr>";
 echo "<div id=\"message\">$message</div>";
 echo'</form>';
 echo'<table>';
 if(isset($_POST['msg'.$emailid])){
                 $qryusersdelete="Delete FROM messages WHERE id='$emailid'";
    $rslusersdelete=mysql_query($qryusersdelete);
                 }
}
}
                    
 
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by Robert07 »

Ok, so for whatever reason, the parameters are being passed in the url instead of the $_POST array. That's ok, you aren't passing passwords or anything sensitive. So if we just take that as a given, then you can get the emailid you want to delete using this:

Code: Select all

 
foreach ($_REQUEST as $key=>$value) {
  if (substr($key,0,3)==="msg") {
    $delId = substr($key,3);
  }
  //here is where you create and execute the sql query to delete the email with the email id that equals $delId
}
 
You might change the first part of your checkbox names to delId or something instead of msg, to make it clear what you plan to do with them.
Regards,
Robert
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by scarface222 »

Hey Robert thanks for the response once again I just have three concerns still. I used your suggested script and modified it by initially setting $delID=NULL because It was coming up undefined, but the script will only delete the first checked message at a time even without that line of code. My two other concerns are first of all I also added a header to refresh the page because otherwise the emails are deleted but do not immediately leave the page unless you refresh again. The header executes no matter what though and I don't like this. I only want it to execute when I delete a msg. I tried putting an if statement for when $delID!=NULL but that messes the script up. Do you have any suggestions for that? Lastly I just am quite curious on why the POST became a GET without me coding it? I will accept this script because you are right, the material is not sensitive and the user can only modify their only profile with the URL but I am just curious if you have any idea.

Code: Select all

$delId=NULL;
  foreach ($_REQUEST as $key=>$value) {
   if (substr($key,0,3)==="msg") {
     $delId = substr($key,3);
   }
   $qryusersdelete="Delete FROM messages WHERE id='$delId'";
    $rslusersdelete=mysql_query($qryusersdelete);
    header("Location: messages.php");
 }
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by Robert07 »

Ok, the reason why you only get one deleted message is because you put the header redirect in the for loop, so after the first deletion you are redirected. You could add a counter in the for loop and check if it is >0 after the loop before redirecting. That would prevent the redirection unless you delete, and also allow multiple deletions:

Code: Select all

 
$delCnt=0;
foreach ($_REQUEST as $key=>$value) {
   if (substr($key,0,3)==="msg") {
     $delId = substr($key,3);
     $qryusersdelete="Delete FROM messages WHERE id='$delId'";
     $rslusersdelete=mysql_query($qryusersdelete);
     $delCnt++;
   }
}
if ($delCnt>0) {
    header("Location: messages.php");
}
 
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by scarface222 »

I still get the problem where I can only delete the first email once at a time even if other boxes are checked. Nothing happens if I check other boxes. Only the first email can be deleted.

Code: Select all

echo '<form action="messages.php" method=\"POST\" ><input type="submit" value="Delete">';
$username=$session->username;
$messages="SELECT * FROM messages WHERE username='$username'";
$query=mysql_query($messages);
while ($row = mysql_fetch_assoc($query)) {
    $from=$row['from'];
 $subject=$row['subject'];
 $date=$row['date'];
 $message=$row['message'];
 $emailid=$row['id'];
 echo "<tr><td><input name=\"msg".$emailid."\" type=\"checkbox\"></td><td>$from</td> <td><a id=\"openbox\">$subject</a></td> <td>$date</td></tr>";
 echo "<div id=\"message\">$message</div>";
 echo'</form>';
 
 $delCnt=0;
 foreach ($_REQUEST as $key=>$value) {
    if (substr($key,0,3)==="msg") {
      $delId = substr($key,3);
      $qryusersdelete="Delete FROM messages WHERE id='$delId'";
      $rslusersdelete=mysql_query($qryusersdelete);
      $delCnt++;
    }
 }
 if ($delCnt>0) {
     header("Location: messages.php");
 }
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by Robert07 »

Try printing the delete query each time and the $_REQUEST array again, to make sure multiple email ids are passed and the queries are correct. If you get multiple delete queries but only one is being executed, then try executing them manually (mysql console or phpmyadmin) and checking for errors.

Code: Select all

 
echo "Request: <BR>";
print_r($_REQUEST);
 
 foreach ($_REQUEST as $key=>$value) {
    if (substr($key,0,3)==="msg") {
      $delId = substr($key,3);
      $qryusersdelete="Delete FROM messages WHERE id='$delId'";
      echo "Now deleting email id $delId with the query $qryusersdelete<br>";
      mysql_query($qryusersdelete);
      $delCnt++;
    }
 }
 
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Delete emails php... expert assistance greatly appreciated

Post by scarface222 »

By default this is displayed

Request:
Array ( [PHPSESSID] => 9gskhjri4o7c6cgkhs697lie55 )

When i check any other box then the first, I do not get any response. When I check the box to delete the first along with any other other combination of checks I just get this message for the number of messages their are which is obviously the id of only one of the messages.

Message repeated

'Now deleting email id 41 with the query Delete FROM messages WHERE id='41''

The id for all the messages is obviously right but I think it may be a problem with the request function but I do not fully understand it so I do not know how to approach it
Post Reply