[英]PHP passing variable through href
I'm having a little problem with passing a parameter for a query to another page. 我在将查询参数传递到另一个页面时遇到了一些问题。
I want to make the name of the restaurant a link that would pass the name of the product to process a query on the next page. 我想将餐厅名称设为一个链接,该链接将传递产品名称以处理下一页上的查询。
echo "<p class='p2'><a class='link' href='restaurant.php?name=". $row['name'] ."'><strong>". $row['name'] ."</strong></a>
on the restaurant page 在餐厅页面上
<?php
require ("db.php");
$name = $_GET['name'];
$query = "SELECT * FROM restaurant WHERE name =\"$name\"";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
?>
but nothing is displayed. 但没有显示任何内容。
Any idea what I'm doing wrong? 知道我在做什么错吗?
First, a note: you should pass an ID rather than the name, since certain characters aren't great in URLs. 首先,请注意:您应该传递ID而不是名称,因为某些字符在URL中不是很好。
Second, try using urlencode()
on the name. 其次,尝试在名称上使用
urlencode()
。
echo "<p class='p2'><a class='link' href='restaurant.php?name=". urlencode($row['name']) ."'><strong>". $row['name'] ."</strong></a>
Of course it won't display anything, because you don't have any print
functions (echo, print, var_dump, ...) in your file. 当然,它不会显示任何内容,因为您的文件中没有任何
print
功能(echo,print,var_dump等)。
Anyways, you probably thought that your query doesn't work. 无论如何,您可能认为您的查询不起作用。 If so, try to
echo
your $row['name']
. 如果是这样,请尝试
echo
$row['name']
。 If everything's OK, check if your variable is set, but it probably isn't because you get null
. 如果一切正常,请检查是否设置了变量,但这可能不是因为您得到了
null
。
To fix that issue, use isset()
or empty()
. 要解决该问题,请使用
isset()
或empty()
。
Example: 例:
if(!empty($_GET['name'])) $name = $_GET['name'];
else die('Variable name is empty');
Try also to add ini_set('display_errors', true)
to the top of your pages to see if there's any errors. 还尝试将
ini_set('display_errors', true)
到页面顶部,以查看是否存在任何错误。
Note that your code is very insecure and vulnerable. 请注意,您的代码非常不安全且容易受到攻击。 Use
mysql_real_escape_string()
before executing queries. 执行查询之前,请使用
mysql_real_escape_string()
。
Example: 例:
$name = mysql_real_escape_string($_GET['name']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.