从mysql db编辑数据

时间:2015-02-05 02:35:23

标签: php html mysql pdo

我正在使用CMS,似乎目前我的编辑代码存在问题,我无法弄清楚问题对我的生活有什么影响,当我提交编辑时,一切都会如同编辑成功,但没有任何更改或提交到数据库。

我一直在尝试许多不同的事情,似乎没有任何区别,我完全迷失在这一点上。

editarticle.php

<?php
ob_start();
session_start();

include_once('includes/connection.php');
include_once('includes/news.php');
include_once('includes/functions.php');

$article = new Article;
$funct = new UserFunctions;

if (isset($_SESSION['logged_in'])) {
    $articles = $article->fetch_all();

    if (isset($_POST['title'], $_POST['content'])) {
        $title = $_POST['title'];
        $content = nl2br($_POST['content']);

        if (empty($title) or empty($content)) {
            $error = 'All fields are required!';
            header('Location: index.php?p=editarticle');
        } else {
            global $pdo;

            $query = $pdo->prepare('UPDATE articles SET article_title = ?, article_content = ? WHERE article_id=?');
            $query->bindValue(1, $title);
            $query->bindValue(2, $content);
            $query->bindValue(3, $id);

            $query->execute();

            header('Location: index.php');
        }
    }

    //check if an article is selected to be edited
    if (isset($_GET['id'])) {
        $id = $_GET['id'];

        $query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ?");
        $query->bindValue(1, $id);
        $query->execute();

        $rows = $query->fetchAll();

        //get the article title and content to put in edit inputs
        foreach ($rows as $row) {
            $id = $row['article_id'];
            $title = $row['article_title'];
            $content = $funct->br2nl($row['article_content']);
        }
?>
<!-- POST -->
<div class="post">
    <div class="topwrap">
        <div class="userinfo pull-left">
            <div class="avatar">
                <img src="images/avatar.jpg" alt="" />
                <div class="status green">&nbsp;</div>
            </div>

            <div class="icons">
                <img src="images/icon1.jpg" alt="" /><img src="images/icon4.jpg" alt="" /><img src="images/icon5.jpg" alt="" /><img src="images/icon6.jpg" alt="" />
            </div>
        </div>
        <div class="posttext pull-left">
            <h2>Edit Article</h2>
            <!-- add Article form start !-->
            <form action="editarticle.php" method="post" autocomplete="off">
                <input type="text" name="title" value="<?php echo $title; ?>" /><br /><br />
                <textarea rows="10" cols="87" name="content" /><?php echo $content; ?></textarea>
                <!-- add article form break !-->
        </div>
        <div class="clearfix"></div>
    </div>                              
    <div class="postinfobot">

        <div class="dateposted pull-right">
                <!-- add article form continue !-->
                <input class="btn btn-primary" type="submit" value="Submit Changes" />
            </form>
            <!-- add article form end !-->
        </div>

        <div class="clearfix"></div>
    </div>
</div>
<!-- POST -->
<?php
    } else {
?>
<!-- POST -->
<div class="post">
    <div class="topwrap">
        <div class="userinfo pull-left">

        </div>
        <div class="posttext pull-left">
            <h2>Select an Article to Edit</h2>
                    <?php foreach ($articles as $article) { ?>
                        <?php echo $article['article_id']; ?> - <a href="index.php?p=editarticle&id=<?php echo $article['article_id']; ?>"><?php echo $article['article_title']; ?></a><br />
                    <?php } ?>
        </div>
        <div class="clearfix"></div>
    </div>                              
    <div class="postinfobot">
        <div class="dateposted pull-right"> </div>

        <div class="clearfix"></div>
    </div>
</div>
<!-- POST -->
<?php
    }
} else {
    header('Location: index.php');
}

?>

包括/ news.php

class Article {
    public function fetch_all() {
        global $pdo;
        $article_status = 1;

        $query = $pdo->prepare("SELECT * FROM articles WHERE article_status = ? ORDER BY article_timestamp DESC");
        $query->bindValue(1, $article_status);
        $query->execute();

        return $query->fetchAll();
    }

    public function fetch_data($article_id) {
        global $pdo;
        $article_status = 1;

        $query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ? AND article_status = ?");
        $query->bindValue(1, $article_id);
        $query->bindValue(2, $article_status);
        $query->execute();

        return $query->fetch();
    }
}

我10年来第一次回到PHP,并且在过去的两年里一直在做很多C#开发。到目前为止,我发现很难解决PHP的问题,因为我已经得到很少或没有错误消息(甚至在我的主机上的error_log中也没有)。

为什么这不能将更改提交到数据库?

2 个答案:

答案 0 :(得分:2)

页面在加载时使用get设置$ id。但是,你再次通过创建一个新的帖子实例将数据发布给自己这个新帖子对$ id

一无所知

所以你需要显式传递$ id(除非它是一个会话变量,你可以使用$ _session变量来检索它)作为表单中的隐藏值 尝试将其添加到表单中:

<input type="hidden" value="<?php echo $id;?>">

答案 1 :(得分:1)

正如@noob指出的那样,您需要在第一个表单中传递文章ID,因为您的UPDATE语句需要它。

因此:

if (isset($_POST['title'], $_POST['content'], $_POST['id'])) {
        $title = $_POST['title'];
        $content = nl2br($_POST['content']);
        $id= $_POST['id'];

以你的形式:

<form action="editarticle.php" method="post" autocomplete="off">
                <input type="hidden" value="<?php echo $id;?>">
                <input type="text" name="title" value="<?php echo $title; ?>" /><br /><br />
                <textarea rows="10" cols="87" name="content" /><?php echo $content; ?></textarea>
                <!-- add article form break !-->
        </div>
        <div class="clearfix"></div>
    </div>                              
    <div class="postinfobot">

        <div class="dateposted pull-right">
                <!-- add article form continue !-->
                <input class="btn btn-primary" type="submit" value="Submit Changes" />
            </form>