0

im trying to update status field in comments table, but it fails.

this is my form:

  <?php
                        if ($result['comment_status'] == "approved") {
                        ?>
                        <form action="" method="post">
                            <input type="hidden" name="comment_id" value="<?php echo $result['comment_id'] ?>">
                            <button type="submit" name="disapproved" class="btn btn-primary btn-sm">DisApprove
                            </button>
                        </form>
                        <?php
                        } else {
                            ?>
                            <form action="" method="post">
                                <input type="hidden" name="comment_id" value="<?php echo $result['comment_id'] ?>">
                                <button type="submit" name="approved" class="btn btn-primary btn-sm">Approve</button>
                            </form>
                            <?php
                        }
                        ?>

and this is my php code (query):

if (isset($_POST['comment_id']) && is_numeric($_POST['comment_id']) && $_POST['comment_id'] > 0) {
                $comment_id = $_POST['comment_id'];
            }

 if (isset($_POST['disapproved'])) {
                $query = $connection->prepare("UPDATE comments SET comment_status = 'approved' WHERE comment_id = $comment_id");

                confirm($query->execute(), "Comment approved successfully", "info");
            }

            if (isset($_POST['approved'])) {
                $query = $connection->prepare("UPDATE comments SET comment_status = 'disapproved' WHERE comment_id = $comment_id");

                confirm($query->execute(), "Comment disapproved successfully", "info");
            }

i don't know why its not update

4
  • Any errors returned by the database server? Commented Mar 3, 2019 at 18:29
  • Your logic seems backwards - if (isset($_POST['disapproved'])) { ... SET comment_status = 'approved' ... } and if (isset($_POST['approved'])) { ... SET comment_status = 'disapproved' ...}. Is that correct? Commented Mar 3, 2019 at 18:38
  • @SpacePhoenix no errors Commented Mar 3, 2019 at 18:44
  • @Sean yea its correct and its work Commented Mar 3, 2019 at 18:45

1 Answer 1

1

too many code duplication...

<form action="" method="post">
    <input type="hidden" name="comment_id" value="<?php echo $result['comment_id'] ?>">
    <input type="hidden" name="status" value="<?=($result['comment_status'] == "approved")?>">
    <button type="submit" class="btn btn-primary btn-sm"><?= ($result['comment_status'] == "approved" ? 'DisApprove' : 'Approve' ) ?></button>
</form>

php code. You should to bind comment_id and status

if (isset($_POST['comment_id']) & ... && isset($_POST['status'])) {
    $comment_id = $_POST['comment_id'];
    $status = $_POST['status'] ? 'disapproved' : 'approved';
    $query = $connection->prepare("UPDATE comments SET comment_status = :status WHERE comment_id = :comment_id");
    $query->bindValue(':comment_id', $comment_id, PDO::PARAM_INT);
    $query->bindValue(':status', $status, PDO::PARAM_STR); 
    confirm($query->execute(), "Comment disapproved successfully", "info");
}

Should work, try it

Sign up to request clarification or add additional context in comments.

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.