I have 3 tables:
-
table
events. In this I have fields such as
event_id, user_id, title, email, location, address, latitude, longitude, description, isapproved -
table
event_time. In this I have fields
show_id, event_id, start_date, start_time, end_date, duration, date, end_time -
table
category:
id, cat_id, event_id
I am inserting data into 3 tables so that if an event of event_id 5 is entered into the event table then the corresponding data will be filled in event_title and category.
While filling event_time table if the events start_time is 2011-12-05 and end_date is 2011-12-07 then event_time table will filled with 3 rows like with same event id and different date:
show_id event_id start_date start_time end_date duration date end_time
11 5 2011-12-05 12:00:00 2011-12-07 03:00:00 2011-12-07 15:00:00
10 5 2011-12-05 12:00:00 2011-12-07 03:00:00 2011-12-06 15:00:00
9 5 2011-12-05 12:00:00 2011-12-07 03:00:00 2011-12-05 15:00:00
The category table will be entered based on
number of categories selected in category drop down list.
id cat_id event_id
25 3 5
24 5 5
Now I want to delete the event from the database based on user choice. If he wants to delete the event of particular date then only in event_time it should get deleted in events, category it should be there.
But if user wants to delete event not based on any date the data from all the table should be removed.
<?php
include_once("webconfig.php");
include_once("webdatabase.php");
$show_id = isset($_REQUEST['show_id'])?trim($_REQUEST['show_id']):"";
$event_id = isset($_REQUEST['event_id'])?trim($_REQUEST['event_id']):"";
if(isset($show_id))
{
$s="select * from event_time where event_id='$event_id'";
$num_rows = mysql_num_rows($s);
die($num_rows);
$exe=$db->query($s);
if($db->row_count()==1)
{
$sql ="Delete events,event_time,category from events join event_time join category on(events.event_id=event_time.event_id and event_time.event_id=category.event_id) where event_time.show_id='$show_id'";
$exe=$db->query($sql);
$successMsg = "deleted Successfully! . ";
$xml = '<?xml version="1.0" encoding="utf-8"?>';
$xml .= '<root>';
$xml .= '<delete>';
$xml .= '<status>True</status>';
$xml .= "<message>$successMsg</message>";
$xml .= '</delete>';
$xml .= '</root>';
echo $xml;
}
else
{
$sql1 ="Delete event_time from event_time where event_time.show_id='$show_id'";
e=$db->query($sql1);
$successMsg = "deleted Successfully! . ";
$xml = '<?xml version="1.0" encoding="utf-8"?>';
$xml .= '<root>';
$xml .= '<delete>';
$xml .= '<status>True</status>';
$xml .= "<message>$successMsg</message>";
$xml .= '</delete>';
$xml .= '</root>';
echo $xml;
}
}
else
{
$xml = '<?xml version="1.0" encoding="utf-8"?>';
$xml .= '<root>';
$xml .= '<delete>';
$xml .= '<status>false</status>';
$xml .= "<message>no data found</message>";
$xml .= '</delete>';
$xml .= '</root>';
echo $xml;
}
?>
this is what the code i have written.
Anyone know how to do this?
You will probably have to do all this in your code. You can use triggers if your database platform supports it, and also you should look into constraints and the cascade options.
An “on delete cascade” constraint for example would automatically handle the case where you delete an event, as it would delete the related records in event_time and category for you.
The simplest solution to start with is to use code though. So in your app, determine what needs cleaning out, and issue the deletes yourself.
To delete all time and event info for one event:
Or obviously to delete just the times:
You’ll need to handle the case where there is only one time, should the event be kept in that case? That’s up to you.