gpt4 book ai didi

php - SQL 不向数据库中插入信息

转载 作者:行者123 更新时间:2023-12-02 07:35:06 25 4
gpt4 key购买 nike

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

如有任何帮助,我们将不胜感激。

最佳答案

你需要转义reserved words in MySQL像带反引号的 range

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

关于php - SQL 不向数据库中插入信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17392626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com