来自数据库的回显字段是否按预期工作?

时间:2014-08-17 20:19:23

标签: php mysql

这是一件非常简单的事情,但由于某些原因,我看不到我无法回应mysql数据库字段中的字段?

以下是我的代码,除了我需要它以回应新更新的投票数量之外,任何帮助都将非常感激。

<?php
require 'core/init.php';
$_SESSION['score'] = $_POST['score'];
$_SESSION['pid'] = $_POST['pid'];

$score  = $_SESSION['score'];
$pid    = $_SESSION['pid'];
$ipaddy = $_SERVER['REMOTE_ADDR'];

$udVote = $db->query("INSERT INTO ratings (score, pid, ip) VALUES ('{$score}','{$pid}','{$ipaddy}')") && $db->query("UPDATE votes SET votecount = votecount +1 WHERE pid='$pid'");

if ($udVote){
    $voteC = $db->query("SELECT votecount FROM votes WHERE pid='$pid' LIMIT 1");
        echo $voteC;
} else {
    echo "error updating database";
}
?>  

1 个答案:

答案 0 :(得分:0)

如果其他人遇到问题并在此处寻找解决问题的方法,我将在下面发布更新后的工作代码。

<?php
require 'core/init.php';

$_SESSION['score'] = $_POST['score'];
$_SESSION['pid'] = $_POST['pid'];

$score  = $_SESSION['score'];
$pid    = $_SESSION['pid'];
$ipaddy = $_SERVER['REMOTE_ADDR'];
$records =array();

if($results = $db->query("SELECT * FROM votes")){
    if($results->num_rows){
        while($row = $results->fetch_object()){
            $records[] = $row;
        }
        $results->free();
    }
}

$udVote = $db->query("INSERT INTO ratings (score, pid, ip) VALUES ('{$score}','{$pid}','{$ipaddy}')") && $db->query("UPDATE votes SET votecount = votecount +1 WHERE pid='$pid'");

if ($udVote){
    if(!count($records)){
        echo 'no records';
    }else{
        foreach ($records as $r){
            echo escape($r->votecount);
        }
        die();
    }
    }else{
        echo "error updating database";
}


?>