duanmao2774 2011-03-22 16:27
浏览 34
已采纳

MySQL语法? 应该很简单!

I am having trouble with the $updatequery SQL, this php script accepts 3 post variables in an ajax call, the first is an array of previously correct answers and not relevant here. $win is 0 or 1 depending on whether the previous question was answered correctly or not and $id is the primary id of the word (it's a word match game, like Korean flashcards). $updatequery is intended to build up statistics for each word, how many people get it right and determine how difficult the word is simply by recording how many times it has been clicked and how many of those times it was answered correctly. If I answer the word wrong it is reflected accurately in the database: 1 click, no wins. However is I answer correctly it says 0 clicks, no wins in the database...what is going wrong?!?!?!

<?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条回答 默认 最新

  • dongzhabo2796 2011-03-22 16:39
    关注

    There is no AND when updating multiple columns. Just seperate them by comma -

    $updatequery = "UPDATE words SET clicks=clicks+1, wins=wins+ '$win' WHERE id= '$id'"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?