【发布时间】:2021-12-28 05:12:10
【问题描述】:
我正在尝试创建一个在 index.php 中显示新帖子的文章发布系统,当您单击一个时,它会将您带到显示整篇文章的 artikkeli-sivu.php。出于某种原因,我无法在 artikkeli-sivu.php 上显示文章,但我可以在 index.php 中显示文章。 artikkeli-sivu.php 给了我这个错误:
警告:未定义的数组键“article_id”在 C:\xampp\htdocs\php-projekti1\artikkeli-sivu.php 在第 74 行。
下面是index.php、connection.php,下面是table
<?php
include_once('connection.php');
include_once('article.php');
$article = new Article;
$artikkelit = $article->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
<title>CMS projekti</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="UTF-8">
</head>
<body>
<div class="container">
<a id="otsikko" href="index.php">CMS</a>
<ol>
<?php foreach ($artikkelit as $artikkelii) { ?>
<li>
<a href="artikkeli-sivu.php?id=<?php echo $artikkelii['article_id']; ?>">
<?php echo $artikkelii['article_title']; ?>
</a><br>
postattu: <?php echo date('l jS', $artikkelii['article_timestamp']); ?>
</li>
<?php } ?>
</ol>
</div>
</body>
</html>
这里是 artikkeli-sivu.php
<!DOCTYPE html>
<?php
include_once 'connection.php';
include_once 'article.php';
$article = new Article;
if(isset($_GET['id'])) {
$id = $_GET['id'];
$data = $article->fetch_data($id);
}
?>
<html>
<head>
<meta charset="UTF-8">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<style>
body, html {
margin: 0;
padding: 0;
text-align: center;
}
.back-button {
position: relative;
top: 500px;
width: 140px;
height: 45px;
border-radius: 25px;
font-size: 18px;
font-family: 'Roboto', arial;
font-weight: 500;
letter-spacing: 2px;
background-color: white;
box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.1);
border: none;
transition: 0.3s ease 0s;
cursor: pointer;
outline: none;
}
.back-button:hover {
background-color: #24a0ed;
box-shadow: 0px 5px 20px #6bbff2;
color: #fff;
transform: translateY(-3px);
}
.article-container {
background-color: white;
width: 500px;
height: 400px;
margin: 0 auto;
box-shadow: 0px 0px 1px 1px lightgrey;
}
h1 {
text-align: center;
font-family: 'Roboto', sans-serif;
font-weight: 500;
}
</style>
</head>
<body>
<h1> Artikkelit </h1>
<div class="article-container">
<?php echo $data['article_id'];?>
</div>
<a class="back-link" href="index.php"><button class="back-button">Back</button></a>
</body>
</html>
这里是文章.php
<?php
class Article {
public function fetchAll(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM artikkelit");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($article_id){
global $pdo;
$query = $pdo->prepare("SELECT * FROM artikkelit WHERE article_id= ?");
$query->bindValue(1, $article_id);
$query->execute();
return $query->fetchAll();
}
}
?>
这里是connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "cms";
try {
$pdo = new PDO('mysql:host=localhost;dbname=cms', 'root', '');
echo "<h3>Yhteys luotu!</h3>";
} catch(PDOException $e) {
exit('Epäonnistui');
}
?>
【问题讨论】:
-
什么是“这个错误”?您能否以文本形式分享错误消息以及您尝试解决的方法?
-
这是错误:警告:第 74 行 C:\xampp\htdocs\php-projekti1\artikkeli-sivu.php 中的未定义数组键“article_id”。我试图检查一切是否在 artikkeli-sivu.php 中正确写入,因为我可以让文章显示在 index.php 中
-
请通过编辑为您的问题添加所有说明。此外,
artikkeli-sivu.php不包含任何可能触发该问题的第 74 行 -
我现在改了
-
您是否检查了
$data的值而不是检查$data['article_id']?也许你的查询由于某种原因没有工作,或者$data是一个对象而不是一个数组。