2010-04-21 88 views
0

問題我編輯的代碼,而現在的頁面加載和一切,但它並沒有插入到數據庫:與MYSQL數據庫

<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> 

回答

3

除了Date保留字所指出的dnagirl:

....VALUES('ADDED','$_POST['Add']'.... 

不能使用['x']這裏。您可以試試:

....VALUES('ADDED','{$_POST['Add']}'.... 

還是這個,這是OK的字符串文字,但有問題的,因爲它外面一個是錯的:

....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

+0

我不知道如何使用intval,但我會查找它 – shinjuo 2010-04-21 19:44:59

+0

仍不知道爲什麼它不工作 – shinjuo 2010-04-21 19:47:25

+0

在該行添加了關於模板問題的更多信息 – bobince 2010-04-21 19:47:59

2
INSERT INTO `log` 
    (`changes`, `amount`, `cardID`, `person`, Date) //PROBLEM: Date is a reserved word 
VALUES 
    ('ADDED','$_POST['Add']','$_POST['Cards']', '$_POST['Person']', NOW()) 

列日期是保留字。引用它或將其更改爲非保留字。

+0

我改變的話,它仍然didnt解決問題 – shinjuo 2010-04-21 19:42:25

+1

請與'的mysql_query錯誤(「插入等...」)或死(mysql_error());' – webbiedave 2010-04-21 19:45:23

0

我要重申已填充的參數值線

"INSERT INTO `log` (`changes`, `amount`, `cardID`, `person`, Date)VALUES('ADDED','$_POST['Add']','$_POST['Cards']', '$_POST['Person']', NOW())" 

,並更換日期當然,然後把它手動在數據庫中。

而且使用的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); 
+0

迴應此行意味着什麼? – shinjuo 2010-04-21 19:51:58