我在一个教程中开始了这些代码片段,然后我将它们与我在互联网上找到的其他教程混合在一起,但现在我在制作“编辑”和“删除”选项时遇到了问题。有人可以帮我吗?这是我的代码片段。
- 管理员(文件夹:add.php、index.php、logout.php)
- 资产(文件夹:style.css)
- 包括(文件夹:article.php,connection.php)
- 文章.php
- 索引.php
索引.php
<?php
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>CMS</title>
<link href='assets/style.css' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="container">
<a href="index.php" id="logo">CMS</a>
<ol>
<?php foreach ($articles as $article) { ?>
<li>
<a href="article.php?id=<?php echo $article['article_id']; ?>">
<img src="news_images/<?php echo $article['article_photo']; ?>.jpg" width="300" height="150" alt="Ver mas" class="article_photo" />
</a>
<br />
<a href="article.php?id=<?php echo $article['article_id']; ?>">
<h2><?php echo $article['article_title'];?>
</a>
-<small><small>
posted <?php echo date('l jS', $article['article_timestamp']); ?>
</small></small>
</h2>
<?php //echo $article['article_content'];?>
<a href="article.php?id=<?php echo $article['article_id']; ?>">
<b>Read More</b>
</a>
<hr>
</li>
<?php }?>
</ol>
<br />
<small><a href="admin" >admin</a></small>
</div>
</body>
</html>
文章.php
<?php
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
if (isset($_GET['id'])) {
$id = $_GET['id'];
$data = $article->fetch_data($id);
?>
<html>
<head>
<title>CMS</title>
<link href='assets/style.css' rel='stylesheet' type='text/css'>
</head>
<body>
<!-- FB like button -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<!-- FB fINISH -->
<div class="container">
<a href="index.php" id="logo">CMS</a>
<img src="news_images/<?php echo $data['article_photo']; ?>.jpg" width="600" height="300" alt="Ver mas" class="article_photo" />
<h2>
<?php echo $data['article_title']; ?>
<small><small><small> - posted <?php echo date('l jS', $data['article_timestamp']); ?></small></small></small>
</h2>
<div class="fb-like" data-href="http://www.wr-audio.com/article.php?id=<?php echo $data['article_id']; ?>" data-send="true" data-width="450" data-show-faces="false" data-font="arial"></div>
<p>
<?php echo $data['article_content']; ?>
</p>
<div class="fb-comments" data-href="http://www.wr-audio.com/article.php?id=<?php echo $data['article_id']; ?>" data-width="470" data-num-posts="10"></div>
<br />
<a href="index.php">← Back</a>
</div>
</body>
</html>
<?php
} else {
header('Location: index.php');
exit();
}
?>
包括/connection.php
<?php
try {
$pdo = new PDO('mysql:host=example.com;dbname=myDatabase', 'username', 'password');
} catch (PDOException $e) {
exit('Database error.');
}
?>
包括/article.php
<?php
class Article {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM articles ORDER BY article_timestamp DESC");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($article_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ?");
$query->bindValue(1, $article_id);
$query->execute();
return $query->fetch();
}
}
?>