我编辑了代码,现在页面加载了所有内容,但它没有插入到数据库中:
<body>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
require("serverInfo.php");
mysql_query("UPDATE `cardLists` SET `AmountLeft` = `AmountLeft` + ".mysql_real_escape_string($_POST['Add'])." WHERE `cardID` = '".mysql_real_escape_string($_POST['Cards'])."'");
echo "\"" .$_POST['Add'] ."\" has been added to the inventory amount for the card \"". $_POST['Cards']. "\"";
mysql_query("INSERT INTO `log` (`changes`, `amount`, `cardID`, `person`, `date`)VALUES('ADDED',".mysql_real_escape_string($_POST['Add']).",
".mysql_real_escape_string($_POST['Cards']).",".mysql_real_escape_string($_POST['Person']).", NOW())") or die (mysql_error());
mysql_close($link);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php require("serverInfo.php"); ?>
<?php
$res = mysql_query("SELECT * FROM cardLists order by cardID") or die(mysql_error());
echo "<select name = 'Cards'>";
while($row=mysql_fetch_assoc($res)) {
echo "<option value=\"$row[cardID]\">$row[cardID]</option>";
}
echo "</select>";
?>
Amount to Add: <input type="text" name="Add" maxlength="8" />
Changes Made By: <select name="Person">
<option value="justin">Justin</option>
<option value="chris">Chris</option>
<option value="matt">Matt</option>
<option value="dan">Dan</option>
<option value="tim">Tim</option>
<option value="amanda">Amanda</option>
</select>
<input type="submit" name ="submit" onClick= "return confirm(
'Are you sure you want to add this amount?');">
</form>
<br />
<input type="button" name="main" value="Return To Main" onclick="window.location.href='index.php';" />
</body>
</html>
答案 0 :(得分:3)
除了dnagirl指出的Date
保留字:
....VALUES('ADDED','$_POST['Add']'....
您无法在此处使用['x']
。你可以试试:
....VALUES('ADDED','{$_POST['Add']}'....
或者这个,在字符串文字中是可以的,但是有问题,因为它在外面是错误的:
....VALUES('ADDED','$_POST[Add]'....
但那仍然是SQL注入。你需要:
....VALUES('ADDED','".mysql_real_escape_string($_POST['Add'])."'....
而且:
"... + ".mysql_real_escape_string($_POST['Add'])." ... "
你没有在该文字周围放置单引号,所以尽管有转义调用,你仍然有SQL注入。要么在它周围加上引号,要么如果你想确保它总是一个整数,请使用intval
。
(参数化查询很好,你知道。)
mysql_close($link);
应该做什么? $link
来自哪里?
... action="<?php echo $_SERVER['PHP_SELF']; ?>" ...
echo "<option value=\"$row[cardID]\">$row[cardID]</option>";
echo "\"" .$_POST['Add'] ."\" has been added ..."
HTML注入(XSS风险)。记住你的htmlspecialchars
。
onClick= "return confirm('Are you sure you want to add this amount?');"
请使用form onsubmit
。
答案 1 :(得分:2)
INSERT INTO `log`
(`changes`, `amount`, `cardID`, `person`, Date) //PROBLEM: Date is a reserved word
VALUES
('ADDED','$_POST['Add']','$_POST['Cards']', '$_POST['Person']', NOW())
列Date是保留字。引用它或将其更改为非保留字。
答案 2 :(得分:0)
我会回应这一行
"INSERT INTO `log` (`changes`, `amount`, `cardID`, `person`, Date)VALUES('ADDED','$_POST['Add']','$_POST['Cards']', '$_POST['Person']', NOW())"
填入参数值,当然更换Date,然后将其手动放入数据库中。
使用mysql_query的返回值,并使用mysql_error:
if (!mysql_query("SELECT * FROM nonexistenttable", $link)) {
echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
}
修改强>
$var="INSERT INTO `log` (`changes`, `amount`, `cardID`, `person`, Date)VALUES('ADDED','$_POST['Add']','$_POST['Cards']', '$_POST['Person']', NOW())";
echo $var; // will show up in logs
mysql_query($var);