Page 1 of 1

Creating an Event System

Posted: Tue Jul 01, 2003 3:55 am
by DarkLightning
I have tried for a while with no success to get this to work and I have tried countless methods and ideas, but I'm hitting roadblocks that are stopping my ideas.

Here is the situation:

I am trying to create a system for adding and deleting events, etc. I have the adding part down fine, but the script for deleting events that I am trying to make is more complicated.

I have the script display the events that meet the criteria that you set and I want there to be a checkbox by each one so you can check which events you want to delete. I have that part down. The part I am having trouble with is having the script delete multiple events that are checked. What I am trying to do is send the values of the check boxes and everything else in the form to another page where I would retrieve the info and have it delete the checked events from the previous page. I don't know if there is a better way to do this, or any way to do it. I'm hitting a blank wall. I'm sure there must be some way to do it, but I would appreciate it if someone could point me in the right direction. If I wasn't clear enough or you need clarification, I will be happy to explain the best I can, of even give you the link to the page I am working on if it would help.

Thanks in advance.

Posted: Tue Jul 01, 2003 4:17 am
by []InTeR[]
Name the checkboxes del[] ie:

Code: Select all

<input type="checkbox" name="deleteї]" value="$id">
Put in the value part the UNIQUE key.

Then the code sould sound like:

Code: Select all

if(isset($_POST["delete"])){
  for($t1=0;$t1<sizeof($_POST["delete"]));$t1++){
    $query = "DELETE FROM `table` WHERE `id`='".$_POST["delete"][$t1]."'";
    if(!mysql_query($query)){
      echo mysql_error()."\n";
      echo $query;
      exit;
    }
  }
}

Thank you

Posted: Tue Jul 01, 2003 4:45 am
by DarkLightning
Thanx for your help. After correcting two slight errors in your script, it works now. Thank you so much.

Maybe you could help me with another question I had. If not, don't worry about it. But, is there an easy way to create a query that will select all events that match your search options taking into account the fact that if a field is left blank, then it should ignore the field. For example.

If I leave the month blank and select the day as the 28th, it would know to search for all events in any month that occur on the 28th. Or if I leave all the fields blank except for the year. You would show all events for that year. I can do this if I make some 12 if-then statements for every situation, but that is definitely not practical. Do you know of an alternative?

Posted: Tue Jul 01, 2003 5:11 am
by Heavy
[]InTeR[] wrote:

Code: Select all

if(isset($_POST["delete"])){
  for($t1=0;$t1<sizeof($_POST["delete"]));$t1++){
    $query = "DELETE FROM `table` WHERE `id`='".$_POST["delete"][$t1]."'";
    if(!mysql_query($query)){
      echo mysql_error()."\n";
      echo $query;
      exit;
    }
  }
}
?>
Personally I would:

Code: Select all

<?php
if(isset($_POST['delete'])){
  foreach ($_POST['delete'] as $DeleteID){
    $whereCondition .= ($whereCondition != '' ? ' or' : '') . ' id= '''.addslashes($DeleteID).'''';
  }
  mysql_query('delete from table WHERE ' . $whereCondition) or die('Deletion query error: ' . mysql_error());
}
?>
I think it looks less messy then, and it makes one query instead of one for each event. :roll:

Posted: Tue Jul 01, 2003 5:21 am
by twigletmac
Heavy wrote:it makes one query instead of one for each event. :roll:
You can then use mysql_affected_rows() to check that the number of records you expected to have been deleted have been.

Mac

Posted: Tue Jul 01, 2003 8:44 am
by Heavy
DarkLightning, I spent some time with checkboxes. You might want to check the result out: