PHP mysql update int


PHP mysql update int

这是我的代码,它向likelist表插入一个user_id和一个post_id。

这部分已经很好用了。

现在,我想在likes列中的表comments中添加一个"+1",它是一个int类型,其中post_id等于第1部分中的值。

我该怎么做?

<?php
//load and connect to MySQL database stuff
require("config.inc.php");
if (!empty($_POST)) {
    //initial query
    $query = "INSERT INTO likelist ( user_id, post_id) VALUES ( :user, :post ) ";
    //Update query
    $query_params = array(
        ':user' => $_POST['user_id'],
        ':post' => $_POST['post_id']

    );
    //execute query
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);

    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());
        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Database Error. Couldn't add post!";
        die(json_encode($response));
    }

    $response["success"] = 1;
    $response["message"] = "Username Successfully Added!";
    echo json_encode($response);


} else {
?>
        <h1>Add Like</h1> 
        <form action="addlike.php" method="post"> 
            User ID:<br /> 
            <input type="text" name="user_id" placeholder="user id" /> 
            <br /><br /> 
            Post ID:<br /> 
            <input type="text" name="post_id" placeholder="post id" /> 
            <br /><br /> 
            <br /><br />
            <input type="submit" value="Add Like" /> 
        </form> 
    <?php
}
?> 

编辑我只想在第一部分成功时发生这种情况,这样可以确保点赞是唯一的,用户不能多次点赞一篇帖子。

:尝试

$update_sql = "update comments set likes = likes + 1 where post_id = :post_id";