Jump to content

Calculating the cost of a booking


matt.sisto

Recommended Posts

Hi all, I would like to work out the sum of booking based upon stored data and user input, so I am pulling data from 2 different tables to work out the fee for a particular service, and then I wish to multiply that by the quantity the user has requested. I have written this script but it just sends out the first error message?

<?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('Y-m-d', $event_end);
    $end_Time =$start_time;

$sql = "SELECT * FROM calendar_events WHERE event_start='".$event_start."' AND event_end>='".$end_date."' AND (con_1='".$con_id1."' OR con_2='".$con_id1."' OR con_3='".$con_id1."') AND (con_1='".$con_id2."' OR con_2='".$con_id2."' OR con_3='".$con_id2."') AND (con_1='".$con_id3."' OR con_2='".$con_id3."' OR con_3='".$con_id3."')";
$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";//This is what it is does every time now
      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."') AND (con_1='".$con_id1."' OR con_2='".$con_id1."' OR con_3='".$con_id1."') AND (con_1='".$con_id2."' OR con_2='".$con_id2."' OR con_3='".$con_id2."') AND (con_1='".$con_id3."' OR con_2='".$con_id3."' OR con_3='".$con_id3."')";
    $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 ("Could not perform query $sql <br />".mysql_error());
} 

//This is the new bit of code, eveything was working fine until I introduced this
if ($con_id1 != null){
   $result = mysql_query("SELECT SUM(rate) FROM consultant WHERE con_id='$con_id1'");
         if($result !=null){
             $rate = mysql_result($result, 0);
            }
           else{     
	       die ("Could not perform query $sql <br />".mysql_error());
            }
	  $result = mysql_query("SELECT SUM(fee) FROM rate_service WHERE service = '".$service."' AND unit ='".$unit."' AND rate='".$rate."'");
     if($result !=null){
             $fee = mysql_result($result, 0);
            }
           else{     
	       die ("Could not perform query $sql <br />".mysql_error());
            }

		$x = (int) $quantity;
		$y = (int) $fee;
		$eventFee1 = $y * $x;
}
echo $eventFee1; // wanted to echo out the value to see if I had it working.

  $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."')";

  $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>

 

Anu help appreciated. 8)

Link to comment
https://forums.phpfreaks.com/topic/156766-calculating-the-cost-of-a-booking/
Share on other sites

I have an if statement that verifies the availability of the consultant number 1, and if they are not available I pass an error in the url back to the form which dipslays an error message on the form, this works fine until I introduce:

 
if ($con_id1 != null){
   $result = mysql_query("SELECT SUM(rate) FROM consultant WHERE con_id='$con_id1'");
         if($result !=null){
             $rate = mysql_result($result, 0);
            }
           else{     
	       die ("Could not perform query $sql <br />".mysql_error());
            }
	  $result = mysql_query("SELECT SUM(fee) FROM rate_service WHERE service = '".$service."' AND unit ='".$unit."' AND rate='".$rate."'");
     if($result !=null){
             $fee = mysql_result($result, 0);
            }
           else{     
	       die ("Could not perform query $sql <br />".mysql_error());
            }

		$x = (int) $quantity;
		$y = (int) $fee;
		$eventFee1 = $y * $x;
}
echo $eventFee1; 

But now with this code added it always returns the error in the url. Hope this explains it a bit better. ???

Now I have made some changes to the script it is passing the error1 through the url as opposed to the error2.  ???

<?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('Y-m-d', $event_end);
    $end_Time =$start_time;

$sql = "SELECT * FROM calendar_events WHERE event_start='".$event_start."' AND event_end>='".$end_date."' AND (con_1='".$con_id1."' OR con_2='".$con_id1."' OR con_3='".$con_id1."') AND (con_1='".$con_id2."' OR con_2='".$con_id2."' OR con_3='".$con_id2."') AND (con_1='".$con_id3."' OR con_2='".$con_id3."' OR con_3='".$con_id3."')";
$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."') AND (con_1='".$con_id1."' OR con_2='".$con_id1."' OR con_3='".$con_id1."') AND (con_1='".$con_id2."' OR con_2='".$con_id2."' OR con_3='".$con_id2."') AND (con_1='".$con_id3."' OR con_2='".$con_id3."' OR con_3='".$con_id3."')";
    $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";//ERROR 2
      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 ("Could not perform query $sql <br />".mysql_error());
} 

  $result = mysql_query("SELECT SUM(rate) FROM consultant WHERE con_id='$con_id1'");
    if($result !=null){
      $rate = mysql_result($result, 0);
    }
    else{     
      die ("Could not perform query $sql <br />".mysql_error());
    }
  $result = mysql_query("SELECT SUM(fee) FROM rate_service WHERE service = '".$service."' AND unit ='".$unit."' AND rate='".$rate."'");
if($result !=null){
      $fee = mysql_result($result, 0);
    }
    else{     
     die ("Could not perform query $sql <br />".mysql_error());
    }
  $x = (int) $quantity;
  $y = (int) $fee;
  $eventFee1 = $y * $x;
  echo $eventFee1; 

  $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."')";

  $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";//ERROR 1
      header($url);
  exit();
}

?>

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

 

It is only when I add this bit of code that the script actually start defunking:

 
$result = mysql_query("SELECT SUM(rate) FROM consultant WHERE con_id='$con_id1'");
    if($result !=null){
      $rate = mysql_result($result, 0);
    }
    else{     
      die ("Could not perform query $sql <br />".mysql_error());
    }
  $result = mysql_query("SELECT SUM(fee) FROM rate_service WHERE service = '".$service."' AND unit ='".$unit."' AND rate='".$rate."'");
if($result !=null){
      $fee = mysql_result($result, 0);
    }
    else{     
     die ("Could not perform query $sql <br />".mysql_error());
    }
  $x = (int) $quantity;
  $y = (int) $fee;
  $eventFee1 = $y * $x;
  echo $eventFee1; 

Any help appreciated. Thanks and regs.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.