我有一个Articles.php页面和一个Single.php页面。 Articles.php运行foreach循环列出所有文章。每篇文章的href锚点是:
<a href="single.php?id=<?php echo $article['id'];
点击文章链接时,网址变为:
example.com/single.php?id=*ID*
我无法在单个页面上抓取该文章ID以显示特定于该ID的MySQL行。建议如下:
$id = filter_var($_GET['id'] ?? false, FILTER_VALIDATE_INT);
if($id !== false){
//show the article, i.e. select * from .... where id = id ...
echo "WORKING";
}else{
//show the error like 404
echo "ERROR";
}
应该是:
$id = $_GET($article['id'])
我无法完成这项工作。
答案 0 :(得分:1)
使用..
将值发送到其他页面<a href="single.php?id=<?php echo $article['id'];?>">Link</a> //missing php close tag here
然后使用
获取它$id = $_GET['id'];
答案 1 :(得分:1)
ok lets try this.
on page 1 => article.php
# we assume
database query here
$query = mysqli_query(//query here);
// we then use a while loop
while($q = $query->fetch_array())
{
echo '<a href="single.php?id='.$q['id'].'">'.$q['article_name'].'</a>';
}
ok on page single.php
# we now have example.com/single.php?id=1 eg.
// there are many ways to grab the id
# option 1
// inside single.php
// method 1
$article_id = isset($_GET['id']) ? (int) $_GET['id'] : "";
// method 2
$article_id2 = "";
if(isset($_GET['id']))
{
$article_id2 = $_GET['id'];
}
// now you have the value from the GET method within your local variable scope
// so choose any of the method above
// both works
hope this helps?
答案 2 :(得分:0)
正如Hek mat所说,你错过了Clossing标签:
<a href="single.php?id=<?php echo $article['id'];?>">Link</a>
但是你的代码也不正确$ _GET ['id']总是给string "1"
而不是int 1
,如果未设置id,则会导致错误。
所以试试这个:
if(isset($_GET['id']) && intval($_GET['id']) > 0){
$id = intval($_GET['id']); // now work with $id its an int now
//show the article, i.e. select * from .... where id = id ...
echo "WORKING";
}else{
//show the error like 404
echo "ERROR";
}