在同一行的 PHP PDO 中继续在 $_GET 上出错
Keep giving errors on $_GET in PHP PDO on the same line
我试了一下,运行这部分代码,但是一直报同样的错误。
在第 14 行,我已经尝试了多种方法来检查连接是否正确完成,但一切似乎都很好。我认为错误在另一行,但我找不到它。 ($id = $_GET['id'];)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "netland";
try {
$conn = new PDO("mysql:host=$servername;dbname=$db", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$id = $_GET['id'];
$data = $conn->query("SELECT * FROM Series WHERE id = " . $id)->fetchAll();
echo "<td><a href='index.php?'>Terug</a><br></td>";
foreach ($data as $row) {
echo "<br>";
echo "<table>";
echo "<h1>" . $row["title"] . ' - ' . $row["rating"] . "</h1>";
echo "<tr>";
echo "<th>Awards:</th>";
echo "<td>" . $row["has_won_awards"] . "<br/></th>";
echo "</tr>";
echo "<th>Seasons:</th>";
echo "<td>" . $row["seasons"] . "<br/></th>";
echo "</tr>";
echo "<th>Country:</th>";
echo "<td>" . $row["country"] . "<br/></th>";
echo "</tr>";
echo "<th>Language:</th>";
echo "<td>" . $row["language"] . "<br/></th>";
echo "</tr>";
echo "</table>";
echo "<br><br>";
echo "<td>" . $row["description"] . "<br/></td>";
echo "</tr>";
}
?>
我希望有人知道如何解决这个问题。
您的 $id
变量未设置。您需要先设置它,然后才能在查询中使用它。
我试了一下,运行这部分代码,但是一直报同样的错误。
在第 14 行,我已经尝试了多种方法来检查连接是否正确完成,但一切似乎都很好。我认为错误在另一行,但我找不到它。 ($id = $_GET['id'];)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "netland";
try {
$conn = new PDO("mysql:host=$servername;dbname=$db", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$id = $_GET['id'];
$data = $conn->query("SELECT * FROM Series WHERE id = " . $id)->fetchAll();
echo "<td><a href='index.php?'>Terug</a><br></td>";
foreach ($data as $row) {
echo "<br>";
echo "<table>";
echo "<h1>" . $row["title"] . ' - ' . $row["rating"] . "</h1>";
echo "<tr>";
echo "<th>Awards:</th>";
echo "<td>" . $row["has_won_awards"] . "<br/></th>";
echo "</tr>";
echo "<th>Seasons:</th>";
echo "<td>" . $row["seasons"] . "<br/></th>";
echo "</tr>";
echo "<th>Country:</th>";
echo "<td>" . $row["country"] . "<br/></th>";
echo "</tr>";
echo "<th>Language:</th>";
echo "<td>" . $row["language"] . "<br/></th>";
echo "</tr>";
echo "</table>";
echo "<br><br>";
echo "<td>" . $row["description"] . "<br/></td>";
echo "</tr>";
}
?>
我希望有人知道如何解决这个问题。
您的 $id
变量未设置。您需要先设置它,然后才能在查询中使用它。