insert into mysql with conditions using php -
i trying insert data database in loop list of people attend meeting need check availability first , if room holding meeting available here code
<html> <body> <?php //try using error reporting on error_reporting(e_all); ini_set('diplay_errors', 'on'); $dbhost = "127.0.0.1"; $dbuser = "root"; $dbpass = ""; $dbname = "mss"; $connection = mysql_connect($dbhost, $dbuser, $dbpass, $dbname); if (mysql_errno()) { die("database connection failed:" . mysql_error() . "(" . mysql_errno() . ")"); } mysql_select_db('mss'); ?> <?php echo $id = (isset($_post['id']) ? $_post['id'] : "hello"); ?> <br> <?php echo $title = (isset($_post['title']) ? $_post['title'] : "hello"); ?> <br> <?php echo $employee = (isset($_post['employee']) ? $_post['employee'] : "hello"); ?> <br> <?php $participant = (isset($_post['participant']) ? $_post['participant'] : "hello"); $starttime = (isset($_post['starttime']) ? $_post['starttime'] : "hello"); $endtime = (isset($_post['endtime']) ? $_post['endtime'] : "hello"); $day = (isset($_post['day']) ? $_post['day'] : "hello"); $room = (isset($_post['room']) ? $_post['room'] : "hello"); $lines = explode("\n", $participant); foreach ($lines $line) { echo $line; $q1 = "select availability e_schedule employee_name='$line' , starttime='$starttime' , day='$day'"; $q2 = "select availability room_schedule r_name = '$room' , starttime='$starttime' , day='$day'"; $result = mysql_query($q1, $connection); $result1 = mysql_query($q2, $connection); if ($result == false) { die(mysql_error()); } if ($result1 == false) { die(mysql_error()); } $info = mysql_fetch_array($result); if ($info['availability'] == 1) { echo ("you can't make meeting @ time, please select day or time"); break; } $info1 = mysql_fetch_array($result1); if ($info1['availability'] == 1) { echo ("you can't make meeting @ room, please select room"); break; } else { $insert_meeting="insert e_schedule (employee_name, starttime, endtime, day, availability, activity_name) values ('$line', '$starttime ', '$endtime', '$day', '1', '$title')"; $insert_result = mysql_query($insert_meeting, $connection ); if($insert_result == false) { die(mysql_error()); } $meeting="insert meeting (title, starttime, endtime, day, participant, room) values ('$title', '$starttime ', '$endtime', '$day', '$participant','$room')"; $meeting_result = mysql_query($meeting, $connection ); if($meeting_result == false) { die(mysql_error()); } $insert_room="insert room_schedule (r_name,m_title, starttime, endtime, day, availability) values ('$room','$title','$starttime ', '$endtime', '$day', '1')"; $insert_result1 = mysql_query($insert_room, $connection ); if($insert_result1 == false) { die(mysql_error()); } echo ("the meeting has been created successfully"); } } ?> <br><?php echo $day = (isset($_post['day']) ? $_post['day'] : "hello"); ?> <br> <?php echo $starttime = (isset($_post['starttime']) ? $_post['starttime'] : "hello"); ?><br> <?php echo $endtime = (isset($_post['endtime']) ? $_post['endtime'] : "hello"); ?><br> <?php echo $room = (isset($_post['room']) ? $_post['room'] : "hello"); ?> <br> </body> </html>
the problem when createing meeting not insert participants w enter 1 participants in advance
i'll start psa: need sanitize inputs. code you've provided terribly vulnerable sql injection.
as specific question: looping through participants, change availability of room during each iteration of loop line:
$insert_room="insert room_schedule (r_name,m_title, starttime, endtime, day, availability) values ('$room','$title','$starttime ', '$endtime', '$day', '1')";
so, after first participant, room considered "booked" , won't able add more participants.
the simple fix? move chunks of code check availability of room ($q2 = "select availability ...
, company) outside of loop.
the better fix? i'd consider breaking different components in loop specific functions. 1 function check if participant available. 1 check if room available. 1 add participants meeting. etc. script flow in fashion:
check if room available | |- room not available. exit , tell user. `- room available. check if participants available. | |- 1 or more participants aren't available. exit , tell user. `- participants available. create meeting | |- failed create meeting. exit error. `- meeting created room. | `- loop through participants , add them meeting.
breaking functions make easier debug, read , organize. if want go down rabbit hole, should write class organize each meeting , related properties , methods.
Comments
Post a Comment