-1

我在一个教程中开始了这些代码片段,然后我将它们与我在互联网上找到的其他教程混合在一起,但现在我在制作“编辑”和“删除”选项时遇到了问题。有人可以帮我吗?这是我的代码片段。


  1. 管理员(文件夹:add.php、index.php、logout.php)
  2. 资产(文件夹:style.css)
  3. 包括(文件夹:article.php,connection.php)
  4. 文章.php
  5. 索引.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">&larr; 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();
    }
}

?>
4

1 回答 1

1

老实说,很难理解你在问什么。我阅读问题的方式是您想要创建editdelete按钮,单击时会调用一些脚本来更新数据库?

好吧,有两种选择:

  1. 使用 jQuery/PHP 的 AJAX
  2. 非 AJAX PHP 脚本

我将举例说明#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();
    }


}
}
于 2013-03-26T15:21:09.260 回答