[英]SQL query fetching data from multiple tables and displaying in html table
我正在开发一个从三个表中获取数据的系统。 我的查询从存储在名为$departure和$destination 的变量中的表单中获取输入。 但是,当我使用这些变量时查询不起作用,但是如果我在 sql 查询中输入结果,那么它会从 sql 中获取数据。 我回应了 $departure 和 $destination 以检查它们是否存储了正确的值,并且在查询中使用时这些值是正确的仍然没有给出结果。 这是不起作用的代码:
$q = "SELECT route.departure, route.destination,buses.busid, buses.busno, dates.Date
FROM buses , route , dates
WHERE buses.route = route.rid && buses.date = dates.id && route.departure= '$departure' &&
route.destination='$destination' && dates.Date = '$date'";;
$query = mysqli_query($conn,$q);
while($res = mysqli_fetch_array($query,MYSQLI_ASSOC))
{
?>
<tr class="text-center">
<td> <?php echo $res['departure']; ?> </td>
<td> <?php echo $res['destination']; ?> </td>
<td> <?php echo $res['busid']; ?> </td>
<td> <?php echo $res['busno']; ?> </td>
<td> <?php echo $res['Date']; ?> </td>
<td> <button class="btn-danger btn" name="book"> <a href="selectseats.php?busID=<?php echo $res['ID']; ?>" class="text-white"> Book </a> </button> </td>
</tr>
<?php
}
}
?>
但是,当我在查询$q中将$departure from 'Lahore'和$destination from 'Islamabad' (它们是存储在我的 sql 数据库中的确切值)替换为$q 时,它会显示结果。 请帮助我,因为我完全坚持这一点。 提前致谢!
可能是引号的不正确使用,请尝试按以下方式连接值:
从:
"SELECT route.departure, route.destination,buses.busid, buses.busno, dates.Date
FROM buses , route , dates
WHERE buses.route = route.rid && buses.date = dates.id && route.departure= '$departure' &&
route.destination='$destination' && dates.Date = '$date'";
到:
"SELECT route.departure, route.destination,buses.busid, buses.busno, dates.Date
FROM buses , route , dates
WHERE buses.route = route.rid && buses.date = dates.id && route.departure= '".$departure."' &&
route.destination='".$destination."' && dates.Date = '".$date."'";
还尝试使用本机 SQL 占位符来转义参数并确保数据库的安全。
尝试这个
"SELECT route.departure,
route.destination,buses.busid,
buses.busno, dates.Date
FROM buses , route , dates
WHERE buses.route = route.rid &&
buses.date = dates.id &&
route.departure=\'" +$departure+"\'&&
route.destination="
\' "+$destination +"\' &&
dates.Date = \'
"+
$date+"\'" ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.