我在教程中启动了这些代码片段,然后我将它们与我在互联网上找到的其他教程混合在一起,但现在我有一个问题需要制作" 编辑"和" 删除"选项。谁能帮助我?这是我的代码片段。
的index.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
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();
}
}
?>
答案 0 :(得分:1)
我说实话,很难理解你在问什么。我读这个问题的方式是你要创建edit
和delete
按钮,点击这些按钮会调用一些脚本来更新数据库吗?
嗯,有两种选择:
我将举例说明#2将如何运作,您可以根据以下代码谷歌#1。
你要把按钮放在一个表格中(让我们做delete
):
<强> HTML:强>
<form method="POST" action="">
<input type="hidden" name="article_id" value="<?php echo $article['article_id']; ?>" />
<input type="submit" value="Delete article" name="delete_article" />
</form>
PHP: - 使用预准备语句显示
if(isset($_POST['delete_article'])){
if(isset($_POST['article_id'])){
$sql = "DELETE FROM table_name
WHERE article_id=?";
if($stmt = $mysqli->prepare($sql)){
$stmt->bind_param("i", $_POST['article_id']);
$stmt->execute();
$stmt->close();
}
}
}