$sql = new mysqli(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
// 1. Find the row in the existing table and get the contents
$query1 = '
SELECT
`tech`,
`eng`,
`dr`
FROM `Cnom`
WHERE
`id` = "'.$sql->real_escape_string($_GET['id']).'"
;';
// Use real_escape_string to sanitize anything that the user could modify
$result1 = $sql->query($query1) or die ("<pre>Query failed:\n$query1</pre>");
// die()ing with the query is always helpful for debugging
$row1 = $result1->fetch_assoc() or die ("<pre>No result returned for id {$_GET['id']}</pre>");
// 2. Insert the contents into the archive
$query2 = '
INSERT INTO `c_archive_table` (
`user`,
`tech`,
`eng`,
`dr`
)
VALUES (
"'.$sql->real_escape_string($_SESSION['username']).'",
"'.$sql->real_escape_string($row1['tech']).'",
"'.$sql->real_escape_string($row1['eng']).'",
"'.$sql->real_escape_string($row1['dr']).'"
);';
$sql->query($query2) or die ("<pre>Query failed:\n$query2</pre>");
// 3. Delete from the original table
$query3 = '
DELETE FROM `Cnom`
WHERE
`id` = "'.$sql->real_escape_string($_GET['id']).'"
;';
$sql->query($query3) or die ("<pre>Query failed:\n$query3</pre>");
這可能是一個良好的開端,依據是什麼我猜你的數據庫表看起來像。順便說一句,我建議,在診斷MySQL問題時,你喜歡在這個例子中使用:用多行縮進寫你的查詢;並使用PHP的die (string)
構造來打印出錯的查詢。然後,您可以清楚地查看查詢以查看任何明顯的語法錯誤,並且MySQL還可以告訴您錯誤發生在哪一行。您也可以將die()
d查詢複製並粘貼到phpMyAdmin中。
更重要的是,這可能不是正確的設置。你應該擁有的,而不是兩個冗餘表中載有幾乎相同的信息,是一個列表archived
列。然後,您只需將archived
更改爲布爾值(true),並檢查是否嘗試訪問它。
例如(僞代碼):
if (accessing_all_records) {
// Access all records that aren't archived
$query = '
SELECT
*
FROM `Cnom`
WHERE
`archived` = 0
;';
}
if (inserting_new_record) {
// Create a new record and set archived to 0 by default (better yet, give it a default value)
$query = '
INSERT INTO `Cnom` (
`field_1`,
...,
`archived`
)
VALUES (
value_1,
...,
0
);';
}
if (archiving) {
// Update the record and set the archived value to 1
$query = '
UPDATE `Cnom`
SET
`archived` = 1
WHERE
`id` = id
;';
}
看在上面的問題的高亮。它會告訴你一個可能的問題。 – andrewsi
另外,請修復代碼中的漏洞SQL注入漏洞。 – 2013-07-09 20:30:18