Jump to content

[SOLVED] Syntax Error with PHP booking validation.


matt.sisto

Recommended Posts

Hello, I am trying to put together some validation for my booking. I have an sql statement checking the start_date and end_date, depending on the length of the booking, or the end_time and start_time if the booking is just for a few hours, however it is saying that ther is a problem with my syntax, any help appreciated:

<?php
session_start();

  if (!isset($_SESSION['username']))
    {
   header("Location: login.php");
   exit();
  }
  
  require "dbconn2.php"; 
  
  $email_address = mysql_real_escape_string($_POST['email_address']);
  $unit = $_POST['unit'];
  $service = $_POST['service'];
  $quantity = $_POST['quantity'];
  $con_id1 = $_POST['con_id1'];
  $con_id2 = $_POST['con_id2'];
  $con_id3 = $_POST['con_id3'];
  $extra_info = $_POST['extra_info'];
  $start_time_hr = $_POST['start_time_hr'];
  $start_time_min = $_POST['start_time_min'];
  $time_sec = '00';
  $address_first_line = mysql_real_escape_string($_POST['address_first_line']);
  $post_code = $_POST['post_code'];
  $country = mysql_real_escape_string($_POST['country']);
  $month = mysql_real_escape_string($_POST["month"]);
  $day = $_POST["day"];
  $year = $_POST["year"];

  $event_start = $year."-".$month."-".$day." ".$_POST["event_start"]; 
  $start_time = $start_time_hr.":".$start_time_min.":".$time_sec." ".$_POST["start_time"];

if ($unit == 'Day' && $quantity >= 1 && $quantity <= 7)
{
    $d = (int) $quantity;
    $event_end = strtotime("+$d Days", strtotime($event_start));
    $end_date = date('D j M Y', $event_end);
    $end_Time =$start_time;

$sql = "SELECT * FROM calendar_events WHERE event_start='".$event_start."' AND event_end>='".$end_date."')";
$result = mysql_query ($sql, $connection) or die ("Could not perform query $sql <br />".mysql_error());
    $row = mysql_fetch_array($result);
    if ($row != null){
      $url = "Location: bookingform.php?error2=true";
      header($url);
      exit();
  }

} 
else if ($unit == 'Hour' && $quantity >= 1 && $quantity <= 7)
{
    $h = (int) $quantity;
    $end_time = strtotime("+$h Hours", strtotime($start_time));
    $end_Time = date('G:i:s', $end_time);
    $end_date = $event_start;

$sql = "SELECT * FROM calendar_events WHERE event_start='".$event_start."' AND (start_time<='".$start_time."' AND end_time>='".$end_time."')";
    $result = mysql_query ($sql, $connection) or die ("Could not perform query $sql <br />".mysql_error());
    $row = mysql_fetch_array($result);
    if ($row != null){
      $url = "Location: bookingform.php?error2=true";
      header($url);
      exit();
  }
}

  $sql = "SELECT client_id FROM client WHERE email_address= '$email_address'";
  $result1= mysql_query($sql, $connection)
  or die ("Couldn't perform query $sql <br />".mysql_error());
  $result = mysql_query ($sql, $connection) or die ("Could not perform query $sql <br />".mysql_error());
  $row = mysql_fetch_array($result);
if ($row != null) {

$result = mysql_query( "SELECT SUM(client_id) FROM client" );
if($result)
{
   $client_id = mysql_result($result, 0);
}
else
{
   die(mysql_error()); // replace with desired error-handling functionality
} 

  $sql = "INSERT INTO calendar_events VALUES (0,'".$event_start."','".$end_date."','".$client_id."','".$service."','".$unit."','".$quantity."','".$start_time."','".$end_Time."','".$con_id1."','".$con_id2."','".$con_id3."','".$address_first_line."','".$post_code."','".$country."','".$extra_info."')";
  echo $sql;

  $result = mysql_query ($sql, $connection)
    or die ("Couldn't perform query $sql <br />".mysql_error());
  header("Location: confirmation.php");
  exit();
}
else {
   $url = "Location: bookingform.php?error1=true";
      header($url);
  exit();
}

?>

<html>
<head>
<title>Book</title>
</head>
<body>
</body>
</html>

Link to comment
Share on other sites

According to the your code, the error should be outputting the query that has a syntax error, so post it here please.

 

It looks like your formatting the date variable as human readable and trying to use that to compare in the database.

 

You need to make sure that the fields in the database are the same format as the data in your PHP code.

Link to comment
Share on other sites

Sorted out the error, there was a wandering ')', but the script is still not working properly.

<?php
session_start();

  if (!isset($_SESSION['username']))
    {
   header("Location: login.php");
   exit();
  }
  
  require "dbconn2.php"; 
  
  $email_address = mysql_real_escape_string($_POST['email_address']);
  $unit = $_POST['unit'];
  $service = $_POST['service'];
  $quantity = $_POST['quantity'];
  $con_id1 = $_POST['con_id1'];
  $con_id2 = $_POST['con_id2'];
  $con_id3 = $_POST['con_id3'];
  $extra_info = $_POST['extra_info'];
  $start_time_hr = $_POST['start_time_hr'];
  $start_time_min = $_POST['start_time_min'];
  $time_sec = '00';
  $address_first_line = mysql_real_escape_string($_POST['address_first_line']);
  $post_code = $_POST['post_code'];
  $country = mysql_real_escape_string($_POST['country']);
  $month = mysql_real_escape_string($_POST["month"]);
  $day = $_POST["day"];
  $year = $_POST["year"];

  $event_start = $year."-".$month."-".$day." ".$_POST["event_start"]; 
  $start_time = $start_time_hr.":".$start_time_min.":".$time_sec." ".$_POST["start_time"];

if ($unit == 'Day' && $quantity >= 1 && $quantity <= 7)
{
    $d = (int) $quantity;
    $event_end = strtotime("+$d Days", strtotime($event_start));
    $end_date = date('D j M Y', $event_end);
    $end_Time =$start_time;
   
   $sql = "SELECT * FROM calendar_events WHERE event_start='".$event_start."' AND event_end>='".$end_date."')";
   $result = mysql_query ($sql, $connection) or die ("Could not perform query $sql <br />".mysql_error());
    $row = mysql_fetch_array($result);
    if ($row != null){
      $url = "Location: bookingform.php?error2=true";
      header($url);
      exit();
  }
      
} 
else if ($unit == 'Hour' && $quantity >= 1 && $quantity <= 7)
{
    $h = (int) $quantity;
    $end_time = strtotime("+$h Hours", strtotime($start_time));
    $end_Time = date('G:i:s', $end_time);
    $end_date = $event_start;
   
   $sql = "SELECT * FROM calendar_events WHERE event_start='".$event_start."' AND (start_time<='".$start_time."' AND end_time>='".$end_time."')";
    $result = mysql_query ($sql, $connection) or die ("Could not perform query $sql <br />".mysql_error());
    $row = mysql_fetch_array($result);
    if ($row != null){
      $url = "Location: bookingform.php?error2=true";
      header($url);
      exit();
  }
}

  $sql = "SELECT client_id FROM client WHERE email_address= '$email_address'";
  $result1= mysql_query($sql, $connection)
  or die ("Couldn't perform query $sql <br />".mysql_error());
  $result = mysql_query ($sql, $connection) or die ("Could not perform query $sql <br />".mysql_error());
  $row = mysql_fetch_array($result);
if ($row != null) {

$result = mysql_query( "SELECT SUM(client_id) FROM client" );
if($result)
{
   $client_id = mysql_result($result, 0);
}
else
{
   die(mysql_error()); // replace with desired error-handling functionality
} 

  $sql = "INSERT INTO calendar_events VALUES (0,'".$event_start."','".$end_date."','".$client_id."','".$service."','".$unit."','".$quantity."','".$start_time."','".$end_Time."','".$con_id1."','".$con_id2."','".$con_id3."','".$address_first_line."','".$post_code."','".$country."','".$extra_info."')";
  echo $sql;

  $result = mysql_query ($sql, $connection)
    or die ("Couldn't perform query $sql <br />".mysql_error());
  header("Location: confirmation.php");
  exit();
}
else {
      $url = "Location: bookingform.php?error1=true";
      header($url);
  exit();
}

?>

<html>
<head>
<title>Book</title>
</head>
<body>
</body>
</html>

Appreciate any help.

Link to comment
Share on other sites

Well this would be the first time I tested the script so there is no bookings in the table, therefore there is no rows it can check anything against however at the moment it is not putting anything into the table, and just ends at the confirmation page.  ???

Link to comment
Share on other sites

Have you tried echoing out some of the values of your variables to see if the time sums are getting their math right? It looks like your WHERE clauses often rely on the value of these time variables.

Link to comment
Share on other sites

When I echo out the $sql:

 

INSERT INTO calendar_events VALUES (0,'2010-6-03 ','Sat 5 Jun 2010','20','8','Day','02','11:00:00 ','11:00:00 ','64','62','','FLAT 8, 17 MILTON RD, BH88LP','BH88LP','UK','HELLO').

 

But nothing is actually put into the table?

Link to comment
Share on other sites

At one point in the $event_date variable you have a user friendly formatted date in the format of:

 

Mon 1 Jan 2009

 

If you are using >= in the query with that, it's not going to work, the MySQL engine can't tell if Mon 1 Jan 2009 is before Tues 2 Jan 2009.

 

Are they fields of type DATE in the database? If so you'll need to use the format YYY-MM-DD to be able to check if they are equal. If you want to check if a date is > or < then you'll need to use DATE_FORMAT() in the query to format the dates as this for example: 20090120 which is the same as YYYY-MM-DD without the hyphen delimiter.

Link to comment
Share on other sites

It is being put into the database now, however, the $end_date (Sat 5 Jun 2010) is not being put into the table, so this suggests I have to convert this date format to the one I am using. How do I do that?

 

INSERT INTO calendar_events VALUES (0,'2010-6-03 ','Sat 5 Jun 2010','20','8','Day','02','11:00:00 ','11:00:00 ','64','62','','FLAT 8, 17 MILTON RD, BH88LP','BH88LP','UK','HELLO').

Link to comment
Share on other sites

INSERT INTO calendar_events VALUES (0,'2009-5-10 ','2009-05-13','20','7','Day','03','09:00:00 ','09:00:00 ','64','62','','FLAT 8, 17 MILTON RD','BH88LP','UK','HELLO').

 

This appears to be working at the moment, but I still need to test it (alot), don't want any bugs.

 

Thanks for all your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.