1

I am tried to read the data from two table but when i tested my php code in postman I got an error.enter image description here

Here is my php code:

<?php 
 
 require_once 'connect.php';
 
 $id = $_POST['id'];  

 if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 die();
 }
 
 $stmt = $conn->prepare("SELECT b.reference_no,b.pickup_date,b.pickup_time,b.rental_hour,b.dropoff_date,b.dropoff_time,v.vehicle_brand,v.vehicles_model,v.vehicle_cc,v.vehicle_transmission FROM booking b INNER JOIN vehicles v ON b.plate_number=v.plate_number WHERE id='$id';");
 
 $stmt->execute();
 
 $stmt->bind_result($reference_no,$pickup_date,$pickup_time,$rental_hour,$dropoff_date,$dropoff_time,$vehicle_brand,$vehicle_model,$vehicle_cc,$vehicle_transmission);
 
 $history = array(); 
 
 while($stmt->fetch()){
 $temp = array();
 $temp['reference_no'] = $reference_no; 
 $temp['pickup_date'] = $pickup_date; 
 $temp['pickup_time'] = $pickup_time;  
 $temp['rental_hour'] = $rental_hour; 
 $temp['dropoff_date'] = $dropoff_date; 
 $temp['dropoff_time'] = $dropoff_time; 
 $temp['vehicle_brand'] = $vehicle_brand; 
 $temp['vehicle_model'] = $vehicle_model; 
 $temp['vehicle_cc'] = $vehicle_cc; 
 $temp['vehicle_transmission'] = $vehicle_transmission; 
 array_push($history, $temp);
 }
  
 echo json_encode($history);
 ?>

The error that I got is in this line

$stmt->execute ();
fatini
  • 27
  • 6
  • Always wort enabling error reporting - https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments – Nigel Ren Dec 07 '18 at 19:01

1 Answers1

2

You need to fix your query with (where B.id = ?), as the column id is probably in both table, throwing an ambiguous column id error. And you need to fix the SQL Injection vulnerability, as you are using a $_POST variable directly..

$stmt = $conn->prepare("SELECT b.reference_no,b.pickup_date,b.pickup_time,b.rental_hour,b.dropoff_date,b.dropoff_time,v.vehicle_brand,v.vehicles_model,v.vehicle_cc,v.vehicle_transmission FROM booking b INNER JOIN vehicles v ON b.plate_number=v.plate_number WHERE b.id=?;");
$stmt->bind_param("i", $id);
$stmt->execute();
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • `i` is for a Integer type. You can read more in the documentation: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – Felippe Duarte Dec 07 '18 at 18:57