首先,從來沒有使用棄用mysql_*
API。
切換到PDO
或mysqli
,都有prepared statements
,這將使您的代碼更安全,當涉及到SQL-Injections
(您的代碼是非常開放的)。
當查詢失敗時,全局函數mysql_error()
將返回最新的mysql錯誤。
獲取有關失敗查詢信息的最簡單方法是在查詢執行後添加or die(mysql_error());
。
與示例代碼:
$result = mysql_query($query) or die(mysql_error());
這將報告您的錯誤並停止執行腳本。
您的sql代碼有點不對(正如RST提到的),您在嘗試設置的值之間缺少逗號。
使用mysqli
和prepared statements
,你的代碼可能看起來是這樣的:
// Using the mysqli object oriented style.
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
// Check so that the myqsli connection didnt have any errors.
if($mysqli->connect_errno) {
die($mysqli->connect_error);
}
// Prepare the statement.
$statement = $mysqli->prepare('UPDATE billdata SET Total=?, Due=? WHERE InvoiceNo=?');
// The question marks is placeholders for the input that will be added in a while.
// Check so that the statement was created correct.
if(!$statement) {
die($mysqli->error);
}
// Bind your parameters (ssi tells mysqli what type of params it is, s = string, i = int).
$statement->bindParams('ssi', $total, $due, $invoceno);
// Execute the statement.
if(!$statement->execute()) {
die($statement->error);
}
// Cleanup.
$statement->close();
$mysqli->close();
是什麼問題? – Noman
什麼問題? 您應該首先使用mysql_connect()連接到您的數據庫; 不要忘記在你的情況下用mysql_real_escape_string()檢查SQL注入。 – jde
add thing done db connected,select db n bla bla with this same method我用保存(插入)詳細信息到表中,所以現在m試圖更新(更新)選定的帳單,但是這種方法不起作用 –