2

嘿,我对 SQL 和 PHP 很陌生,但我写了这个,但它似乎不能正常工作;它回显了“成功”,但是当我检查数据库以查看信息是否已提交时,它在表中没有返回任何内容。

function update($script_name, $version, $runtime, $status, $sessionID, $ranged, $attack, $defense, $strength, $magic)
{
    global $db;
    $sql = "SELECT * FROM Dashboard WHERE script_name = '$script_name'";
    try {
        $results = $db->query($sql);
        if ($results->rowCount() <= 0) {
            $query = "INSERT INTO Dashboard (script_name, version, runtime, status, session_id, range, attack, defense, strength, magic) VALUES ('$script_name', '$version', '$runtime', '$status', '$ranged', '$sessionID', '$attack', '$defense', '$strength', '$magic')";
            $db->exec($query);
        } else {
            foreach ($results as $row) {
                $timerunnew = $row['runtime'] + $runtime;
                $v1new = $row['script_name'] + $script_name;
                $v2new = $row['version'] + $version;
                $v3new = $row['status'] + $status;
                $v4new = $row['range'] + $range;
                $v5new = $row['attack'] + $attack;
                $v6new = $row['defense'] + $defense;
                $v7new = $row['strength'] + $strength;
                $v8new = $row['magic'] + $magic;
                $v9new = $row['session_id'] + $sessionID;
            }
            $db->exec("UPDATE Dashboard SET `runtime` = $timerunnew, `script_name` = $v1new, `version` = $v2new, `status` = $v3new, `range` = $v4new, `attack` = $v5new, `defense` = $v6new, `strength` = $v7new, `magic` = $v8new, `session_id` = $v9new  WHERE script_name = '$script_name'");
        }
    } catch (PDOException $ex) {
        echo "fail";
    }
}

任何帮助将非常感激。

4

1 回答 1

7

您需要像反引号一样转义MySQL 中的保留字range

INSERT INTO Dashboard (script_name, ..., `range`, ...) VALUES (...)
                                         ^-----^--------here
于 2013-06-30T16:50:12.067 回答