我正在使用从三个表中获取数据的系统。我的查询从存储在名为$ 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
}
}
?>
但是,当我更换$出发,从“拉合尔”和$目标从“伊斯兰堡”(这是存储在我的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占位符来替换参数并保持数据库的安全。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句