2015-03-30 23:45:30 -04:00
< ? php
include " db_config.php " ;
$conn = mysqli_connect ( $servername , $username , $password , $db );
$unit = mysqli_real_escape_string ( $conn , $_POST [ 'unit' ]);
$start_date_time = mysqli_real_escape_string ( $conn , $_POST [ 'start_date_time' ]);
$description = mysqli_real_escape_string ( $conn , $_POST [ 'description' ]);
$is_ongoing = mysqli_real_escape_string ( $conn , $_POST [ 'is_ongoing' ]);
$end_date_time = mysqli_real_escape_string ( $conn , $_POST [ 'end_date_time' ]);
$alert = mysqli_real_escape_string ( $conn , $_POST [ 'alert' ]);
$user = mysqli_real_escape_string ( $conn , $_POST [ 'user' ]);
//Insert event to events table
$event = " INSERT INTO events (unit_id, date_time_start, description, is_ongoing, date_time_end, alert_id, user) VALUES (' $unit ',' $start_date_time ', ' $description ', ' $is_ongoing ', ' $end_date_time ', ' $alert ', ' $user ') " ;
2015-04-05 17:56:56 -04:00
//MySQL queries to get Unit Name and Alert Name for the Email
$unitname_query = " SELECT unit_name FROM units WHERE unit_id= " . $_POST [ 'unit' ] . " " ;
$unitname_query_run = mysqli_query ( $conn , $unitname_query );
$unitname_array = mysqli_fetch_assoc ( $unitname_query_run );
$unitname = $unitname_array [ 'unit_name' ];
$alertname_query = " SELECT alert_name FROM alerts WHERE alert_id= " . $_POST [ 'alert' ] . " " ;
$alertname_query_run = mysqli_query ( $conn , $alertname_query );
$alertname_array = mysqli_fetch_assoc ( $alertname_query_run );
$alertname = $alertname_array [ 'alert_name' ];
//If successful, redirect back to index.php and send email, else tell user that it failed.
2015-03-30 23:45:30 -04:00
$result = mysqli_query ( $conn , $event );
if ( $result ){
2015-04-05 17:56:56 -04:00
echo ( " Event added, redirecting... " );
sleep ( 2 );
header ( 'Location: ../index.php' );
//Get inserted Event ID
$event_id = $conn -> insert_id ;
//Set Email Info
$to = " TO-EMAIL@DOMAIN.COM " ;
$subject = " New BMS Alert: " . $unitname . " " . $alertname . " " ;
$headers = " From: FROM-EMAIL@DOMAIN.COM " ;
$message = " BMS Unit: " . $unitname . " \n Type of Alert: " . $alertname . " \n Start Date / Time: " . $start_date_time . " \n End Date / Time: " . $end_date_time . " \n Description: " . $description . " \n Created by: " . $user . " \n Event Link: https://DOMAIN.com/bms/viewevent.php?eventid= $event_id \n \n This message generated by https://DOMAIN.com/bms " ;
//WordWrap the message
$message_wrapped = wordwrap ( $message , 70 , " \n " , true );
//Send the email
mail ( $to , $subject , $message_wrapped , $headers );
2015-03-30 23:45:30 -04:00
} else {
2015-04-05 17:56:56 -04:00
echo ( 'Error! Please <a href="javascript:history.back()">go back</a> and try again' );
2015-03-30 23:45:30 -04:00
}
2015-04-05 17:56:56 -04:00
$conn -> close ();
2015-03-30 23:45:30 -04:00
?>