简体   繁体   中英

how to update string value in php with pdo

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM