我正在一个网站上工作,管理员可以在该网站上编辑他们已经创建的时间表。他们可以单击日程表上的任何项目进行编辑。例如,他们可以单击班次开始时间,然后将他们定向到可以更新值的页面。
不幸的是,我无法让它适用于每个价值。似乎文本值工作得很好,但是当它是数字时出现语法错误。
这是我用来更新的内容:
$type = $_GET['type'];
$value = $_GET['value'];
$week = $_GET['week'];
$newval = $_POST['newval'];
if(strlen($newval) > 0)
{
include '../dbinfo.php';
$type = $mysqli->real_escape_string($_POST['type']);
$week = $mysqli->real_escape_string($_POST['week']);
$tablename = $mysqli->real_escape_string("cs" . $_SESSION['squadron']);
$newval = $mysqli->real_escape_string($newval);
if((is_numeric($newval)))
{
$sql = "UPDATE $tablename SET $type=$newval WHERE week=$week";
}
else
{
$sql = "UPDATE $tablename SET $type='$newval' WHERE week=$week";
}
if($result = $mysqli->query($sql))
{
echo "Your specififed changed was completed successfully!<br>";
echo "<a href='edit.php?week=" . $week . "'>Continue editing</a>";
}
else
{
echo mysqli_error($result);
}
}
更改字符串会导致 sql 语句:
UPDATE cs14 SET shift_1_name='Test' WHERE week=1 (this works)
更改数字会导致 sql 语句:
UPDATE cs14 SET shift_ 1_starttime=940 WHERE week=1 (this doesn't work)
它给了我 MySQL 错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1_starttime=940 WHERE week=1' at line 1
我已经研究过这个错误,并且一遍又一遍地检查语法。它在 phpmyadmin 中也不起作用。我不知道接下来要检查什么!
任何人都可以在这里帮我解决我的语法吗???谢谢!