MySQL语法?应该很简单!

时间:2011-03-22 16:27:06

标签: php mysql sql

我在使用$ updatequery SQL时遇到问题,这个php脚本在ajax调用中接受3个post变量,第一个是先前正确答案的数组,在这里不相关。 $ win为0或1,具体取决于前一个问题是否得到正确回答,而$ id是该单词的主要ID(这是一个单词匹配游戏,如韩国抽认卡)。 $ updatequery旨在为每个单词建立统计数据,有多少人正确地确定单词,并通过记录单词被点击的次数以及正确回答的次数来确定单词的难度。如果我回答错误这个词,它会在数据库中准确反映:1次点击,没有获胜。但是我正确回答它说0次点击,数据库没有胜利......出了什么问题?!?!?!

<?php

$dbcon = mysql_connect("localhost", "tulesblo_tules", "Gromit554?") or die(mysql_error());
mysql_select_db("tulesblo_koreangame", $dbcon) or die(mysql_error());
mysql_query("SET NAMES utf8");

$correctarray = explode(",", $_POST["correct"]);
$win = (int)$_POST["win"];
$id = (int)$_POST["id"];
$correct = "";
$y = 1;

if ($id > 0) {
    $updatequery = "UPDATE words SET clicks=clicks+1 AND wins=wins+ '$win' WHERE id= '$id'";
    mysql_query($updatequery) or die(mysql_error());
    }

foreach($correctarray as $x){
    $correct .= " id != " . $x;
    if ($y == count($correctarray)) {break;} 
    $correct .= " AND"; $y++;
    }   

//pick 4 random pairs
$filter = count($correctarray) > 1?"WHERE" . $correct:"";
$querystring = "SELECT * FROM words ".$filter." ORDER BY RAND() LIMIT 4";

$query = mysql_query($querystring);

//turn them into multiarray
$x = 0;
$multiarray = array();
while ($result = mysql_fetch_assoc($query)){
    $multiarray[$x] = array(
        "id"        => $result['id'], 
        "korean"    => $result['korean'], 
        "english"   => $result['english']
        );
    $x++;
}
array_push ($multiarray,$win,$id); 
//send as JSON to client
//array_push($multiarray, $querystring, count($correctarray));
echo json_encode($multiarray);

?>

1 个答案:

答案 0 :(得分:2)

更新多个列时没有AND。只需用逗号分隔它们 -

$updatequery = "UPDATE words SET clicks=clicks+1, wins=wins+ '$win' WHERE id= '$id'"