Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    updated booking code

    hi

    the last booking code was a mess so ive tried to change it but i still seem to be getting an error with my query which is:

    You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'then = 1 else = 0' at line 3

    line 3 is just somewhere in query2

    wot i need the code to do is
    make sure that no-one can double book
    check if the start date is before the end date (this works fine)
    check if the dates are available according to the accommID
    if the dates are available then put the details into the booking table (the insert query which also works)



    Code:
    if(isset($_POST['submit'])){
    require_once("config.php");
    $connection = @mysql_connect($db_host, $db_user, $db_password) or die("oops! error connecting");
    mysql_select_db($db_name, $connection);
    
    
             $accommID = $_POST ["accommID"];
             $book_start = $_POST ["booking_start_date"];
             $book_end = $_POST ["booking_end_date"];
             $flag = $_POST ["flag"];
             
    
            $date1 = $book_start; //booking start date
            $date2 = $book_end; //booking end date
    
            $timestamp1 = strtotime($date1);
            $timestamp2 = strtotime($date2);
    
                    //if the start date is after the end date
                    if($timestamp1 > $timestamp2){
                      echo "Please enter a Booking Start Date that is before the Booking End Date";
                            }
    
                            //if the start date is before the end date
                            elseif ($timestamp1 < $timestamp2){
                            
                            //check if the dates given are both before or both after the dates that are already in the database then the accomm is available
                            //set the flag to 1 so that no-one else can book for them same dates while this customer is going through the booking stages
                            $query2 = "select $accommID from booking group by $accommID
                            having sum(case when ((booking_start_date < $book_start && booking_end_date < $book_end) || (booking_start_date > $book_start && booking_end_date > $book_end)
                            then $flag = 1 else $flag = 0";
                            $result2 = mysql_query ($query2) or die(mysql_error());
    
                            echo "The dates are available";
    
                            }//closes elseif
    
                elseif (!$result2){
                    echo "The dates are not available";
                }
    
                $query = "INSERT INTO booking (booking_start_date, booking_end_date, accommID, flag) VALUES ('$book_start','$book_end','$accommID','$flag')";
                $result = mysql_query($query) or die (mysql_error());
                 if(!$result){
                 echo "Booking not added
                 </td>
                </table>";
                  }
    
                 echo "</td>
                </table>";
    }
    
    else {
    $accommid = $_POST['accommID'];
    
    ?>
        <form action ="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <input type=hidden name=accommID value="<?=$accommid?>">
        <p align="center"> </p>
        <p align="center"><u>Booking Start Date (YYYY/MM/DD):</u>    <input type="text" name="booking_start_date" size="20"></p>
        <p align="center"><u>Booking End Date (YYYY/MM/DD) :</u>    <input type="text" name="booking_end_date" size="20"></p>
        <p> </p>
        <p><input type="submit" name=submit value="Submit"></p>
      </form>
    </td>
    </table>
    <?
    
    } //closes else above
    hope ppl can understand wot im trying to implement
    thanks

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello.
    have you tried printing your query strings to screen so you can see exactly what you are calling. could be the case of a missing variable

  • #3
    New Coder
    Join Date
    Apr 2006
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    hi
    yeh i tried that but it wont even run it because there is a probelem with the syntax of the query itself

    it just gives me the error:

    You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'then = 1 else = 0' at line 3

    i dont even have a line in the code that looks exactly like that!

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts
    you have: then $flag = 1 else $flag = 0";
    and from what error your getting i would assume that $flag isnt being called correctly

  • #5
    New Coder
    Join Date
    Apr 2006
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeh ur rite
    it recognises the correct accommId but it doesnt seem to recognise the flag field

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts
    can i ask, where is the $_POST ["flag"] coming from?

  • #7
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    puja, I answered your other question. Please don't create multiple threads asking basically the same question. It is easier for people to help you where they can see the responses other folks have given you in a single thread.

  • #8
    New Coder
    Join Date
    Apr 2006
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry guelphdad,
    i'll keep that in mind, i understand that its difficult if things are explained again in a newer thread without having the old responses to refer to.

    its just that i have been struggling with this for a long time now and i really need to get it working.

    king2k5, i changed the $_POST ["flag"] to flag = 0 as that is what it should start off as and i am not calling it from any previous pages.

    i dont think the query that i was trying to use was right for wot i am trying to do anyway so i need to create a new one that will actually check the dates in the way i am trying to. they have already selected the accomm they want from the previous pages so i just need to check if the dates they have entered for the chosen accomm are available or not

    sorry again


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •