2

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?

13
  • when you clicked the edit button, is the data displayed? Commented Feb 5, 2015 at 2:41
  • 2
    You should set your error mode to throw exceptions if you haven't already - $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Commented Feb 5, 2015 at 2:42
  • 1
    @Meta but did you run a try {} catch(PDOException $e) {} around the update block? Commented Feb 5, 2015 at 2:45
  • 1
    @Meta You need to change the $db variable to your actual database handler variable which looks like it'd be $pdo. Also, you want to echo $e->getMessage(); Commented Feb 5, 2015 at 2:54
  • 1
    @Darren Thanks for the tip, hopefully this will help me in the future for debugging and troubleshooting issues! Commented Feb 5, 2015 at 3:03

2 Answers 2

2

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;?>">
Sign up to request clarification or add additional context in comments.

3 Comments

if this works, I will feel really dumb, because I had already had this at one point and removed it thinking it wasn't needed if i could just set $id without needing a hidden input).
This worked, however I also needed to add $id = $_POST['id']; above where i set the new title. I feel so dumb for removing that and then forgetting about it.
ofcourse @Meta you need a post variable to retrieve it
1

As @noob pointed out, yu need to pass the article id in the first form, because your UPDATE statement need it.

Therefore:

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

And in your form:

<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.