使用UPDATE MySqli PHP增加Int值

时间:2017-03-19 02:55:24

标签: php html mysql database mysqli

我是SQL和PHP的新手,我正在努力创建一个基本的'喜欢'系统,每次按下like按钮时,int计数都保存在db中。我已经在线查看,但似乎无法发现我做错了什么。它适用于创建新记录,但似乎无法增加预先存在的条目的值。我确定它很小。

<form action="post-like.php" method="POST">
  <input type="hidden" name="post_id" value="post1"></input>
  <button type="Submit">Like!</button>
</form>


// Create connection
$conn = mysqli_connect($hostname, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$postid = $_POST['post_id'];
$sqlcheck = "SELECT * FROM  `likes`WHERE  `postid` =  '$postid'LIMIT 0 , 30";
$checkpost = mysqli_query($conn, $sqlcheck);
$check_post = mysqli_fetch_row($checkpost);

if ($check_post > 0) {
    $sqlike = ("UPDATE likes SET postlikes = postlikes + 1 WHERE postid = '".$postid."'");
    echo "<script>alert('" . $postid . "');</script>";

    if (mysqli_query($conn, $sqllike)) {
        echo "New like created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

} else {
    $sqlnewpost = "INSERT INTO  `tentoesdown`.`likes` (`postid` ,`postlikes`)VALUES ('$postid',  '1');";
    echo "<script>alert('ya2');</script>";

    if (mysqli_query($conn, $sqlnewpost)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}

database table 'likes'

enter image description here

无法找到此错误!

1 个答案:

答案 0 :(得分:0)

更改此行:

if ($check_post > 0) {

为:

if ( (count($check_post) > 0 ) {

mysqli_fetch_row将始终返回数组,而不是数字。