如何插入和更新

时间:2014-04-20 18:30:02

标签: php sql

我已经在此处检查了stackoverflow并在谷歌搜索,但无法找到我的答案。我创建了一个评级系统拇指向上和向下,只有成员可以对一篇文章进行投票。当一个成员投票时,sql正在更新行voteup / votedown。现在我已经制作了一个名为vote的新表格我已经有5列id, user, uID, ctitle, cID

所以如果一个成员以ex:Admin登录并且他赞成文章为ex:I like coding SQL INSERT INTO,那么这个表名为vote Admin user标题为ctitle,但同时需要更新名为postvote_up or vote_down

的表格

我只收到错误:投票失败,

<?php
include 'config.php';
require_once 'core/init.php';
$user = new User();
$username = $user->data()->username;
function getAllVotes(mysqli $db, $id)
{
    $votes = array();
    if($q_13 = "SELECT * FROM post WHERE id = $id");

    if($r_26 = $db->query($q_13));
    if($r_26->num_rows==1) 
    {
        $stem = $r_26->fetch_object();
        $votes[0] = $stem->votes_up;
        $votes[1] = $stem->votes_down;
    }
    return $votes;
}

function getEffectiveVotes(mysqli $db, $id)
{
    $votes = getAllVotes($db, $id);
    $effectiveVote = $votes[0] - $votes[1];
    return $effectiveVote;
}

$id = $_POST['id'];
$action = $_POST['action'];

//current votes
$cur_votes = getAllVotes($db, $id);

//update votes


if($action=='vote_up') 
{
    $votes_up = $cur_votes[0]+1;
    $q_13 = "UPDATE post SET votes_up = $votes_up WHERE id = $id";
    $q_13 = "INSERT INTO vote WHERE user = '$username', cID = '$id'";
}
elseif($action=='vote_down')
{
    $votes_down = $cur_votes[1]+1;
    $q_13 = "UPDATE post SET votes_down = $votes_down WHERE id = $id";
    $q_13 = "INSERT INTO vote WHERE user = '$username', cID = '$id'";
}

    $r_26 = $db->query($q_13);
    if($r_26)
    {
        $effectiveVote = getEffectiveVotes($db, $id);
        echo $effectiveVote." Archers";
    }
    elseif(!$r_26)
    {
        echo "Failed to vote!";
    }

?>

0 个答案:

没有答案