-3

I just want to find some answer on my problem the error said that the query is undefined variable but i already put all the column needed to my table. here is the html file.

<?php
session_start();
include_once("../CORE/dbconfig.php");

if(isset($_GET['book']))
{
    $start_date   = $_GET["start_date"];
    $end_date   = $_GET["end_date"];
    $vehicle_id = $_GET["vehicle_id"];

 if(isset($_GET['customer_id'])){
 $customer_id = $_GET['customer_id'];

 if(isset($_GET['vehicle_id'])){
 $vehicle_id = $_GET['vehicle_id'];
 $rate = $_GET['rate'];

 $start_date->format('Y-m-d');
 $end_date->format('Y-m-d');

$query = mysqli_query($connect, "INSERT INTO booking (start_date, end_date, rental_amount, vehicle_id, customer_id)
    VALUES ('".$start_date."', '".$end_date."', '".$rate."', '".$vehicle_id."', '".$customer_id."')")
    or die ("ERROR: " .mysqli_error($connect));
}
}
}
if($query){
    echo'<script>
    alert("Record Updated!");
    window.location.href="homebooking.php";
    </script>';
}    
else{
    echo'<script>
    alert("Cannot Be Book!");
    window.location.href="homebooking.php";
    </script>';

}

?>
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56

3 Answers3

1

I'm guessing $_GET['book'] isn't set, causing $query not to get initialized and resulting in an error at if($query){. Use if(isset($query)){.

Soronbe
  • 906
  • 5
  • 12
0
  if( isset($_GET['customer_id']) && isset($_GET['vehicle_id']) && isset($_GET['rate']) ){
          $customer_id = $_GET['customer_id'];
          $vehicle_id = $_GET['vehicle_id'];
          $rate = $_GET['rate'];
          $start_date->format('Y-m-d');
          $end_date->format('Y-m-d');

       $query = mysqli_query($connect, "INSERT INTO booking (start_date, end_date, rental_amount, vehicle_id, customer_id)
VALUES ('".$start_date."', '".$end_date."', '".$rate."', '".$vehicle_id."', '".$customer_id."')")
or die ("ERROR: " .mysqli_error($connect));

       if($query){
                     echo'
                         <script>
                           alert("Record Updated!");
                           window.location.href="homebooking.php";
                        </script>
                         ';
        }else{
                 echo'
                       <script>
                        alert("Cannot Be Book!");
                        window.location.href="homebooking.php";
                       </script>
                     ';

        }


  } 

try this way of check vars . i didn't change any thing in the code i just change the way of check on the variables i use this way to be sure the vars what i need already defined and found the values of it

Shooter
  • 51
  • 6
  • i didn't change any thing in the code i just change the way of check on the variables i use this way to be sure the vars what i need already defined and found the values of it – Shooter Mar 14 '17 at 09:15
  • Ok, Thanks for your support :) – Shooter Mar 14 '17 at 09:20
0

This will avoid your error notice:

include_once("../CORE/dbconfig.php");
$query = false; // this is the default set value, 
// which is then only updated if GET books is set, below:
if(isset($_GET['book']))
{
   ...

All of these $_GET values, you should perhaps look into using forms and POST values instead, as well as using mysqli_real_escape_string to minimise risk.

As well as this you should look at using urlencode/urldecode on your GET variables, too.

Martin
  • 22,212
  • 11
  • 70
  • 132