Hello everybody,
I am working with a php calendar that enables users to add events with times and dates directly into a database by clicking on the date on the calendar and submitting details. However at present the function has no way of preventing user submitting an event that is at the same time as another event in the calendar, its just submits as normal.
What I am trying to do it to run a check of the table and see if there is a starttime already existing that has the same values as the new event that is about to be added. Two events cannot be on the same day at the same time.
The following is the function that I am working with and trying to tweek;
function addCalendar($st, $et, $sub, $ade){
$ret = array();
try{
$db = new DBConnection();
$db->getConnection();
//checker
$sqlcheck = "select count (*) from `listings`
WHERE (`Starttime` < '"
.php2MySqlTime(js2PhpTime($et))."') AND (`Endtime` > '"
.php2MySqlTime(js2PhpTime($st))."')";
$handler = mysql_query($sqlcheck);
if ($handler>0){
//then alert the user that this is a duplicate
$ret['Msg'] = 'DUPLICATE';
}else{
//go ahead input the new event
$sql = "insert into `listings` (`subject`, `starttime`, `endtime`, `isalldayevent`) values ('"
.mysql_real_escape_string($sub)."', '"
.php2MySqlTime(js2PhpTime($st))."', '"
.php2MySqlTime(js2PhpTime($et))."', '"
.mysql_real_escape_string($ade)."' )";
//echo($sql);
if(mysql_query($sql)==false){
$ret['IsSuccess'] = false;
$ret['Msg'] = mysql_error();
}else{
$ret['IsSuccess'] = true;
$ret['Msg'] = 'add success';
$ret['Data'] = mysql_insert_id();
}
}catch(Exception $e){
$ret['IsSuccess'] = false;
$ret['Msg'] = $e->getMessage();
}
return $ret;
}
}