用外键获取数据时出现问题

时间:2018-12-07 18:41:04

标签: php

我试图从两个表中读取数据,但是当我在邮递员中测试php代码时出现错误。enter image description here

这是我的php代码:

<?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);
 ?>

我得到的错误在这一行

$stmt->execute ();

1 个答案:

答案 0 :(得分:2)

您需要使用UserService来修正查询,因为两个表中的列(where B.id = ?)可能都在列中,并引发id错误。而且,由于您直接使用ambiguous column id变量,因此您需要修复SQL Injection漏洞。

$_POST