dragon321723 2015-02-05 02:35
浏览 35
已采纳

从mysql db编辑数据

I am working on a CMS and seem to be having issues currently with my edit code and I can't figure out what the problem is for the life of me, when I submit to edit, everything goes through as if the edit was successful, however nothing is ever changed or submitted to the database.

I have been trying many different things and nothing seems to make any difference, I am totally lost on this one.

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');
}

?>

includes/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();
    }
}

I am getting back into PHP for the first time in 10 years and have been doing a lot of C# development over the last 2 years. I am finding it very difficult to troubleshoot issues with PHP thus far, as I have gotten very little or no error messages to work with (not even in the error_log on my host).

Any ideas why this isn't submitting the changes to the database?

  • 写回答

2条回答 默认 最新

  • duanbi8089 2015-02-05 02:45
    关注

    The page is seting $id with get when loaded. But then again you are posting the data to self by creating a new instance of post this new post doesn't know anything about $id

    So you need to explicitly pass $id (unless it is a session variable, where you can use $_session variable to retrieve it) as a hidden value in your form try adding this to form:

    <input type="hidden" value="<?php echo $id;?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作