PHP代码中的投票系统错误

时间:2018-01-25 17:30:22

标签: php mysql

我正在尝试建立一个投票系统,利用php与我的数据库进行通信,以更新我的标签中的单元格。行。但是,我编写的代码在运行时显示此错误"致命错误:未捕获PDOException:SQLSTATE [HY093]:无效的参数编号:绑定变量的数量与标记的数量不匹配。"任何帮助将不胜感激。

if($vote == 'up' && strpos($row['tags'], ','. $id . '_down,') !== false){
    //Switch vote from down to up
    $query = "UPDATE POSTS SET vote_up = vote_up + 1, vote_down = vote_down - 1 WHERE post_id = :id";
    $stmt = $dbh->prepare($query);
    $stmt->bindValue(':id', $id);
    $stmt->execute();

    //Output vote
    $query = "SELECT post_id, vote_up, vote_down FROM POSTS WHERE post_id = :id";
    $stmt = $dbh->prepare($query);
    $stmt->bindValue(':id', $id);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    echo $row['vote_up'] - $row['vote_down'];

    //Update records with user_id and tags
    $query = "SELECT *FROM VOTES WHERE user_id = :user_id";
    $stmt = $dbh->prepare($query);
    $stmt->bindValue(':user_id', $user_id);
    $stmt->execute();

    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $tags = str_replace(',' . $id . '_down,', ',' . $id . '_up,', $row['tags']);
    $query = "UPDATE VOTES SET tags = :tags WHERE user_id = :user_id";
    $stmt->bindValue(':user_id', $user_id);
    $stmt->bindValue(':tags', $tags);
    $stmt->execute();
}

1 个答案:

答案 0 :(得分:0)

您错过了上次查询中的prepare()声明:

$row = $stmt->fetch(PDO::FETCH_ASSOC);
    $tags = str_replace(',' . $id . '_down,', ',' . $id . '_up,', $row['tags']);
    $query = "UPDATE VOTES SET tags = :tags WHERE user_id = :user_id";

    $stmt = $dbh->prepare($query);   // ADD THIS!!!

    $stmt->bindValue(':user_id', $user_id);
    $stmt->bindValue(':tags', $tags);
    $stmt->execute();