我最近发了一篇文章,但我想我没有很好地解释它。
我有一个产品页面,url是http:.......rest_id=3/area='Enfield‘。我一直在我的网站上使用类似的查询,但这个查询不起作用,尽管它之前工作了大约2分钟。
我使用GET语句来获取url中的区域名称,然后使用区域名称从数据库中获取正确的数据,例如电话号码、twitter和Facebook句柄。但是什么都没有显示出来。
我有错误处理程序,我唯一的错误是通知:未定义变量: twit in...对于所有3个变量,即使它们已被声明。
你知道为什么会发生这种情况吗?
if (isset($_GET['area'])) {
$franc_dets = $_GET['area'];
$get_franc_d = "SELECT * FROM Franc_dets WHERE Fran_City = '$franc_dets'";
mysqli_stmt_execute($run_get_franc_d);
$iii = 1;
while ($row_get_fram = mysqli_fetch_array($run_get_franc_d)) {
$fran_phone = $row_get_fram['Fran_Contact_Num'];
$twit = $row_get_fram['Twitter'];
$iii++;HTML
......
<td class='phn_dets'>
<p id='phn_title'>Problems ordering?</p>
<p id='phn_numb'><?php echo $fran_phone ?></p>
</td>
<td class='collapse'>
<img src='./Images/franc_dets_twitter.png' height='50' width='50' alt='Twitter logo'>
</td>
<td class='twitter_dets'>
<p id='sm_title'>Social media</p>
<a id='sm_twit' href='https://twitter.com/<?php echo $twit ?>'>@<?php echo $twit ?></a>
</td>
<td class='collapse'>
<img src='./Images/franc_dets_fb.png' height='50' width='50' alt='Facebook logo' >
</td>
<td class='fb_dets'>
<a id='sm_fb' href='https://www.facebook.com/<?php echo $fb; ?>'><?php echo $fb; ?></a>发布于 2016-05-08 11:15:03
在while循环中声明$twit。如果您的查询没有返回任何结果,则从未定义过$twit,因此您会看到这样的通知。在你的例子中,我看到了两个原因,为什么即使你的数据库中有数据,你也得不到结果:
确保正确处理没有返回结果的情况,这可以很简单,只需在while循环外部给$twit一个默认值,或者告诉用户没有结果。
此外,您在查询中放置变量的方式使您的网站容易受到SQL injection的攻击。我建议你读一下这个主题。使用带占位符的准备好的查询。请参阅PHP manual中的示例
https://stackoverflow.com/questions/37095533
复制相似问题