2014-10-30 85 views
0

我正在構建一個簡單的網頁,允許用戶從數據庫中刪除記錄,然後頁面將重新加載刪除的記錄。無法找出用於完成此操作的代碼。這是我的主網頁:用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); 
?> 

基本上當用戶點擊刪除按鈕,該特定的記錄將被刪除,主頁將刷新記錄被刪除。真的不知道代碼,謝謝!

+0

第一次使用準備好的語句,列引號是反引號而不是單引號 – Ghost 2014-10-30 01:34:27

+1

我很確定'link'不是一個有效的表單方法。您應該使用'POST'並添加一個隱藏字段,其中包含要修改/刪除的記錄的ID。 – jeroen 2014-10-30 01:36:25

+2

'mysql_query($ sql);'那個不行。在'MUSIC'中加入'$ sql =「DELETE'WHERE'id'= $ id」;' - 點#1:混合API。第二點:錯誤的標識符。另外,Jeroen說。 – 2014-10-30 01:37:18

回答

0

你delete_process.php的末尾添加頁眉

<?php 
//redirect to index page 
header('Location:index.php'); //replace index.php with the page you want to redirect to 
?> 

應該工作

0

您需要

'<input type="hidden" name="id" value="' . $row['id'] . '" />' 

添加到形式的職位,以delete_process.php。

但是,您應該知道,您發佈的代碼非常不安全,而且通過SQL注入進行攻擊並刪除整個數據庫將會很輕鬆。

相關問題