need help with an if statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jordan Boden
    New Member
    • Dec 2010
    • 13

    #1

    need help with an if statement

    Hi,

    I have a problem trying to prevent a time slot and seat number being double booked. I am using PHP and MySQL for this. Currently I have no errors concerning variable names or issues with inserting data into the database. The only problem I seem to have involves preventing a certain seat number and time from being double booked. I have tried numerous things and I think the problem lies within my 'if' statement so i have shown a snippet of this below:

    Code:
     
    
    $sql="SELECT * FROM swinging_ship WHERE Time='$time' AND SeatNumber='$seatnumber'";
    $sqlresult=mysql_query($sql) or die("Could not retreive data from table");
    
    $row=mysql_fetch_array($sqlresult);
    
    if ($row['Time']=='$time' AND $row['SeatNumber']=='$seatnumber')
    {
    echo "<br />Please Go back this seat is taken\n";
    }
    
    else
    {
    $sqlstatement="INSERT INTO customer_details (Title, FirstName, MiddleName, LastName, HouseNoName, Address, Town, County, Postcode, TelephoneNumber, Email)
    										   VALUES ('$title','$firstname','$middlename','$lastname','$housenoname','$address','$town','$county','$postcode','$number','$email')";
    							$sql_result=mysql_query($sqlstatement,$connection) or die("Couldn't execute the SQL statement");
    
    $sqlstatement="INSERT INTO swinging_ship (RideSelection, Time, SeatNumber)
    								   		   VALUES ('$rideselection','$time','$seatnumber')";
    							$sql_result=mysql_query($sqlstatement,$connection) or die("Couldn't execute the SQL statement");
    
    print "<br />This seat is free - proceed";
    
    }
    Like I said all my variable names and SQL commands are working and the data is being inserted into the correct database tables and fields. The only problem is preventing the time and seat number from being double booked. I am a noob when it comes to PHP and MySQL so any feedback is much appreciated.

    Thanks,

    Jordan
  • AutumnsDecay
    New Member
    • Mar 2008
    • 170

    #2
    You have quotes around your variables in the if statement. Your statement was also formatted incorrectly for multiple comparisons.

    And while it's not "wrong", I would put logical operators instead of words (ie. '&&' instead of 'AND').

    This is what it should look like.

    Code:
    if (($row['Time'] == $time) && ($row['SeatNumber'] == $seatnumber))
    {
    echo "<br />Please Go back this seat is taken\n";
    }
     
    else
    {
     ...
    }

    Comment

    • Jordan Boden
      New Member
      • Dec 2010
      • 13

      #3
      Thanks very much AutumnsDecay your answer worked for me!

      Comment

      Working...