我已经在“阅读更多”页面上工作了很长一段时间。我基本上想要的是当你点击阅读更多时,它将重定向到新闻项页面。
的index.php
$p=mysqli_query($con, "SELECT id, titel, tekst, datum FROM nieuws LIMIT $start, $limit");
if(mysqli_num_rows($p) > 0) {
while ($row = mysqli_fetch_array($p)){
$nieuws = $row['0'];
echo "<h2>".$row['1']."</h2>";
echo "<div class='clr'></div>";
echo "<p>".$row['2']."</p>";
echo "<p> Geplaatst op: ".$row['3']."</p>";
echo "<a href='pages/nieuws.php?nieuwsitem=$nieuws'>Lees meer..</a></br></br>";
}
} else {
echo "Geen items gevonden.";
}
nieuws.php
<?php
$con = mysqli_connect("localhost", "root", "", "groenevingers");
if (mysqli_connect_errno()) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$p=mysqli_query($con, "SELECT id, titel, tekst, datum FROM nieuws WHERE id='$nieuws'");
if(mysqli_num_rows($p) > 0) {
while ($row = mysqli_fetch_array($p)){
echo "<h2>".$row['1']."</h2>";
echo "<div class='clr'></div>";
echo "<p>".$row['2']."</p>";
echo "<p> Geplaatst op: ".$row['3']."</p>";
}
} else {
echo "Item niet gevonden.";
}
?>
在'nieuws.php'中,它应该显示带有id的特定新闻项。但是,我收到一条错误消息“通知:未定义的变量:nieuws”。 我可以理解为什么它说错误,因为我没有定义$ row ['0']这是我的新闻表中的新闻ID,但我无法以某种方式解决它。
无论如何你们可以帮帮我吗? 非常感谢帮助。
亲切的问候, vaxzz
答案 0 :(得分:1)
您可能需要更改新闻页面的代码,如下所示:
<?php
$con = mysqli_connect("localhost", "root", "", "groenevingers");
if (mysqli_connect_errno()) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
if(isset($_GET['nieuwsitem'])) {
$nieuws = intval($_GET['nieuwsitem']);
$p=mysqli_query($con, "SELECT id, titel, tekst, datum FROM nieuws WHERE id='$nieuws'");
if(mysqli_num_rows($p) > 0) {
if ($row = mysqli_fetch_array($p)){
echo "<h2>".$row['1']."</h2>";
echo "<div class='clr'></div>";
echo "<p>".$row['2']."</p>";
echo "<p> Geplaatst op: ".$row['3']."</p>";
}
} else {
echo "Item niet gevonden.";
}
} else {
echo "Item niet gevonden.";
}
?>
这将做几件事:
if(isset($_GET['nieuwsitem'])) {
检查您的网址中是否有ID
$nieuws = intval($_GET['nieuwsitem']);
检索该ID并将其转换为整数,因此它安全&#34;要在您的查询中使用,它还会设置您的$nieuws
变量
答案 1 :(得分:1)
您必须在使用之前定义变量$ nieuws: nieuws.php: -
<?php
$con = mysqli_connect("localhost", "root", "", "groenevingers");
$nieuws = $_GET['nieuwsitem'];
if (mysqli_connect_errno()) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$p=mysqli_query($con, "SELECT id, title, text, datum FROM nieuws WHERE id='$nieuws'");
if(mysqli_num_rows($p) > 0) {
while ($row = mysqli_fetch_array($p)){
echo "<h2>".$row['1']."</h2>";
echo "<div class='clr'></div>";
echo "<p>".$row['2']."</p>";
echo "<p> Geplaatst op: ".$row['3']."</p>";
}
} else {
echo "Item niet gevonden.";
}
?>