SQL没有将信息插入数据库

时间:2013-06-30 16:47:20

标签: php sql

嘿,我对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";
    }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:7)

您需要使用反引号{/ 1>来reserved words in MySQL转义range

INSERT INTO Dashboard (script_name, ..., `range`, ...) VALUES (...)
                                         ^-----^--------here