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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀