title: Booking system with php and mysql
body: <p>So I'm producing this booking system for supplementary lessons in school,and I'm new to coding.So I'm experiencing many problems when making this.<br>
  I'm using PHP and mysql for my system.The following is my tedious 100-line-code for 'sinsert.php'</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;style&gt;
  .error {color: #FF0000;}
  &lt;/style&gt;
      &lt;title&gt;Booking Page&lt;/title&gt;
      &lt;link rel="stylesheet" type="text/css" href="style.css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;?php
  //Prevent empty fields
  $date = $room = $tid = $subj = $start = $end = $noofstu = null;
  $nodate = $noroom = $notid = $nosubj = $nostart = $noend = $nonoofstu = null;
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["date"])) {
      $nodate = "*Date of lesson is required";
      $date = null;
    } else {
      $date = $_POST["date"];
    }
  
    if (empty($_POST["room"])) {
      $noroom = "*Room is required";
      $room = null;
    } else {
      $room = $_POST["room"];}
  
    if (empty($_POST["tid"])) {
      $notid = "*Teacher in charge is required";
      $tid = null;
    } else {
      $tid = $_POST["tid"];}
  
      if (empty($_POST["noofstu"])) {
      $nonoofstu = "*Number of Students Attending is required";
      $noofstu = null;
    } else {
      $noofstu = $_POST["noofstu"];}
  }
  function ($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
  return $data;}
  ?&gt;
  
  &lt;div id='frm'&gt;&lt;form action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;" method="POST"&gt;
      &lt;p&gt;&lt;h1&gt;&lt;u&gt;Lesson Booking Form&lt;/u&gt;&lt;/h1&gt;&lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;Date of Lesson:&lt;/label&gt;
          &lt;input type="date" name="date" &gt;
          &lt;br&gt;&lt;span class="error"&gt;&lt;?php echo $nodate;?&gt;&lt;/span&gt;
      &lt;/p&gt;
          &lt;p&gt;
              &lt;label&gt;Room:&lt;/label&gt;&lt;br&gt;
              &lt;input type="radio" name="room" value="246"/&gt; Room 246
              &lt;input type="radio" name="room" value="340"/&gt; Room 340
              &lt;input type="radio" name="room" value="342"/&gt; Room 342&lt;br&gt;
              &lt;span class="error"&gt; &lt;?php echo $noroom;?&gt;&lt;/span&gt;
          &lt;/p&gt;
          &lt;p&gt;             
              &lt;label&gt;Teacher in charge:&lt;/label&gt;&lt;br&gt;
              &lt;input type="radio" name="tid" value="T001"/&gt;Mr.Williams
              &lt;input type="radio" name="tid" value="T002"/&gt;Mr.Zimmerman
              &lt;input type="radio" name="tid" value="T003"/&gt;Ms.Alcott&lt;br&gt;
              &lt;span class="error"&gt; &lt;?php echo $notid;?&gt;&lt;/span&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;label&gt;Subject:&lt;/label&gt;
              &lt;select name="subj"&gt;            
              &lt;option value="unitA"&gt;Unit A&lt;/option&gt;
              &lt;option value="unitB"&gt;Unit B&lt;/option&gt;
              &lt;option value="unitC"&gt;Unit C&lt;/option&gt;
              &lt;option value="unitD"&gt;Unit D&lt;/option&gt;
              &lt;option value="unitE"&gt;Unit E&lt;/option&gt;
              &lt;option value="unitF"&gt;Unit F&lt;/option&gt;
              &lt;/select&gt;
          &lt;/p&gt;
          &lt;p&gt; 
              &lt;label&gt;Starting Time:&lt;/label&gt;
              &lt;select name="start"&gt;   
              &lt;option value='09:00:00'&gt;09:00&lt;/option&gt;
              &lt;option value='10:00:00'&gt;10:00&lt;/option&gt;
              &lt;option value='11:00:00'&gt;11:00&lt;/option&gt;
              &lt;option value='12:00:00'&gt;12:00&lt;/option&gt;
              &lt;option value='13:00:00'&gt;13:00&lt;/option&gt;
              &lt;option value='14:00:00'&gt;14:00&lt;/option&gt;
              &lt;option value='15:00:00'&gt;15:00&lt;/option&gt;
              &lt;option value='16:00:00'&gt;16:00&lt;/option&gt;
              &lt;option value='17:00:00'&gt;17:00&lt;/option&gt;
              &lt;/select&gt;
              &amp;nbsp;&amp;nbsp;&amp;nbsp;
              &lt;label&gt;Ending Time:&lt;/label&gt;
              &lt;select name="end"&gt; 
              &lt;option value='10:00:00'&gt;10:00&lt;/option&gt;
              &lt;option value='11:00:00'&gt;11:00&lt;/option&gt;
              &lt;option value='12:00:00'&gt;12:00&lt;/option&gt;
              &lt;option value='13:00:00'&gt;13:00&lt;/option&gt;
              &lt;option value='14:00:00'&gt;14:00&lt;/option&gt;
              &lt;option value='15:00:00'&gt;15:00&lt;/option&gt;
              &lt;option value='16:00:00'&gt;16:00&lt;/option&gt;
              &lt;option value='17:00:00'&gt;17:00&lt;/option&gt;
              &lt;option value='18:00:00'&gt;18:00&lt;/option&gt;
              &lt;/select&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;label&gt;Number of Students Attending:&lt;/label&gt;
              &lt;input type='number' name='noofstu' min='1'max='40'&gt;&lt;br&gt;
              &lt;span class="error"&gt;&lt;?php echo $nonoofstu;?&gt;&lt;/span&gt;
          &lt;/p&gt;
          &lt;p&gt;
          &lt;input type="reset" id="reset" value="Reset"&gt;
          &lt;input type="submit" id="submit" value="Submit"&gt;
          &lt;/p&gt;
          &lt;/form&gt;
  &lt;?php
  //Insert data in mysql database
  $date = $_POST['date'];
  $room = $_POST['room'];
  $tid = $_POST['tid'];
  $subj = $_POST['subj'];
  $start = $_POST['start'];
  $end = $_POST['end'];
  $noofstu = $_POST['noofstu'];
  
  
      $conn = @mysqli_connect("localhost","root","","sba");
      if (mysqli_connect_errno()) {
          die("Failed to connect to MySQL: " . mysqli_connect_error());
      }
  
      $sql = "INSERT INTO booking (date, room, tid, subj, start, end, noofstu) Values (?,?,?,?,?,?,?)";
      $stmt = mysqli_prepare($sql);
      $stmt-&gt;bind_param( $_POST['date'], $_POST['room'], $_POST['tid'], $_POST['subj'], $_POST['start'], $_POST['end'], $_POST['noofstu']);
      $stmt-&gt;execute();
  
      if(!mysqli_query($conn, $sql))
      {
          echo "Not Inserted!";
      }
      else
      {
          echo "Inserted";
      }
  
  
  
  $conn-&gt;close();
  ?&gt;
  
          &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>There are two codes in 'sinsert.php'.The first code is to make sure the user filled in all fields before submitting or else error codes will appear.The first code is mostly copied from W3schools.So I don't think there will be any problems regarding this code.<br>
  The second code is to insert the inputted data into the 'booking' table in the 'sba' database.I made code with the help of my teacher's code and <a href="https://stackoverflow.com/questions/37367992/php-inserting-values-from-the-form-into-mysql">PHP: Inserting Values from the Form into MySQL</a> <br>
  The 'booking' table consists of 8 fields.Besides the 7 fields mentioned in the code,there's also primary key field 'bookid' which is in auto increment<br><br>
  Here are the problems I encountered<br><br>
  1.Everytime I went to the 'sinsert.php' page,I got 2 warning messages beneath my form<br>
  The first warning message states:<br> 'mysqli_prepare() expects exactly 2 parameters, 1 given in on line 143'<br>
  The other is fatal error message that states: <br>'Uncaught Error: Call to a member function bind_param() on null in sinsert.php:144 Stack trace: #0 {main} thrown in sinsert.php on line 144'<br>
  And I have no idea what they mean or how to solve them<br><br>
  2.I also cannot insert the inputted data in the database.My guess is due to the error messages above.But I don't actually know the real reason<br><br>
  I'd like to know how can I solve the above problems.I'm really grateful if you helped<br><br><br>
  Bonus Question:How can I prevent double booking,like no records should have the same room and same time</p>