title: sql query to check availability of rooms between check in and check out dates using php form
body: <p>I have 2 tables in my database. First table is room_details and second table is booking_details.</p>
  
  <p><code>room_details</code> table:</p>
  
  <pre><code> Room_Id |  Category  |  Type    |  Price
  ---------|------------|----------|---------
   R 001   |   DELUXE   |  NON-AC  |  2500
           |            |          |
   R 002   |   DELUXE   |  AC      |  3500
           |            |          |
   R 003   |   GRAND    |  NON-AC  |  4500
           |            |          |
   R 004   |   GRAND    |  AC      |  5500
           |            |          |
   R 005   |   SUITE    |  NON-AC  |  6500
           |            |          |
   R 006   |   SUITE    |  AC      |  7500
  </code></pre>
  
  <p><code>booking_details</code> table:</p>
  
  <pre><code> Room_Id  |  Booking_Id  |  Booking_Date  | Check_in_Date |  Check_out_Date
  ----------|--------------|----------------|---------------|-----------------
    R 001   |    B 001     |   2017-03-25   |   2017-03-26  |   2017-03-28
            |              |                |               |
    R 001   |    B 002     |   2017-03-25   |   2017-03-29  |   2017-04-01
            |              |                |               |
    R 004   |    B 003     |   2017-03-25   |   2017-03-26  |   2017-03-30
            |              |                |               |
    R 005   |    B 004     |   2017-03-25   |   2017-03-26  |   2017-03-28
  </code></pre>
  
  <p>Query:</p>
  
  <pre><code>$sql  = 'SELECT * FROM room_details
           WHERE room_id NOT IN(
                 SELECT room_id FROM booking_details
                 WHERE '$check_in' BETWEEN Check_in_Date AND Check_out_Date
                   OR '$check_out' BETWEEN Check_in_Date AND Check_out_Date
           )';
  </code></pre>
  
  <p>When I take the check_in and check_out dates as input from user using php to display all the available rooms then it displays a error as given below :</p>
  
  <blockquote>
    <p><strong>Parse error</strong>: syntax error, unexpected '$check_in' (T_VARIABLE)</p>
  </blockquote>
  
  <p>What is wrong with this query?</p>
  
  <p>This is the complete php code:</p>
  
  <pre><code>&lt;?php
  
    $a = $_POST["data_1"];
    $b = $_POST["data_2"];
  
    $check_in = date("Y-m-d", strtotime($a));
    $check_out = date("Y-m-d", strtotime($b));
  
    $dbhost = "localhost:3306"; 
    $dbuser = "root"; 
    $dbpass = ""; 
    $dbname="hotel";
  
    //create connection
    $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  
    //check connection 
    if( $conn-&gt;connect_error ) { 
      die("Connection failed:" . $conn-&gt;connect_error);
    }
  
    $sql  = 'SELECT * FROM room_details
             WHERE room_id NOT IN(
               SELECT room_id FROM booking_details
               WHERE '$check_in' BETWEEN Check_in_Date AND Check_out_Date
                 OR '$check_out' BETWEEN Check_in_Date AND Check_out_Date
             )';
  
    $result = $conn-&gt;query($sql);
  
   if ($result-&gt;num_rows &gt; 0) {
     echo "&lt;table border=1&gt;
             &lt;tr&gt;
               &lt;th&gt;Room_Id&lt;/th&gt;
               &lt;th&gt;Category&lt;/th&gt;
               &lt;th&gt;Type&lt;/th&gt;
               &lt;th&gt;Price&lt;/th&gt;
             &lt;/tr&gt;";
  
     // output data of each row
     while($row = $result-&gt;fetch_assoc()) {
       echo "&lt;tr&gt;";
       echo "&lt;td&gt;" . $row['Room_Id'] . "&lt;/td&gt;";
       echo "&lt;td&gt;" . $row['Category'] . "&lt;/td&gt;";
       echo "&lt;td&gt;" . $row['Type'] . "&lt;/td&gt;";
       echo "&lt;td&gt;" . $row['Price'] . "&lt;/td&gt;";
       echo "&lt;td&gt;" . "&lt;a href = room_booking.php&gt; BOOK NOW &lt;/a&gt;" . "&lt;/td&gt;";
       echo "&lt;/tr&gt;";
     }
     echo "&lt;/table&gt;";
   } else {
     echo "Error:" . $sql . "&lt;br&gt;" . $conn-&gt;error;
   }
   $conn-&gt;close();
  ?&gt;
  </code></pre>