PHP mysql更新int

时间:2015-03-28 20:30:23

标签: php mysql

这是我的代码,它将user_id和post_id插入到表格列表中。

此部分已经正常工作。

现在我想在列中为注释添加“+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
}

?> 

修改 我只希望在第一部分成功时发生这种情况,这样可以确保喜欢它是唯一的,并且用户不能多次像一个帖子一样。

1 个答案:

答案 0 :(得分:0)

:尝试

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