我正在構建一個簡單的網頁,允許用戶從數據庫中刪除記錄,然後頁面將重新加載刪除的記錄。無法找出用於完成此操作的代碼。這是我的主網頁:用php刪除記錄,然後重新加載頁面
<html>
<head>
<title>Change Record form</title>
<style type="text/css">
td {font-family: tahoma, arial, verdana; font-size: .875em }
</style>
</head>
<body>
<a href="add_new_form.php">Add new record</a>
<br>
<br>
<?php
$con=mysqli_connect("localhost", "root", "", "customers");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$strSQL = "SELECT * FROM music";
$rs = mysqli_query($con, $strSQL);
while($row = mysqli_fetch_array($rs, MYSQLI_BOTH)) {
// Write the value of the column FirstName (which is now in the array $row)
echo "<br>";
echo "<br>";
echo $row['artist'] . "<br />";
echo $row['title'] . "<br />";
echo $row['format'] . "<br />";
echo $row['notes'] . "<br />";
echo '<FORM METHOD="LINK" ACTION="update_form.php">
<INPUT TYPE="submit" VALUE="Update">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="delete_process.php">
<INPUT TYPE="submit" VALUE="Delete">
</FORM>';
}
?>
然後這是delete_process頁:
<?php
$id = $_GET['id'];
$artist = $_GET['artist'];
$title = $_GET['title'];
$format = $_GET['format'];
$notes = $_GET['notes'];
//create connection to DB
$con=mysqli_connect("localhost", "root", "", "customers");
//check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "DELETE from 'MUSIC' WHERE 'id' = $id";
mysql_query($sql);
?>
基本上當用戶點擊刪除按鈕,該特定的記錄將被刪除,主頁將刷新記錄被刪除。真的不知道代碼,謝謝!
第一次使用準備好的語句,列引號是反引號而不是單引號 – Ghost 2014-10-30 01:34:27
我很確定'link'不是一個有效的表單方法。您應該使用'POST'並添加一個隱藏字段,其中包含要修改/刪除的記錄的ID。 – jeroen 2014-10-30 01:36:25
'mysql_query($ sql);'那個不行。在'MUSIC'中加入'$ sql =「DELETE'WHERE'id'= $ id」;' - 點#1:混合API。第二點:錯誤的標識符。另外,Jeroen說。 – 2014-10-30 01:37:18