2014-04-20 69 views
-1

刪除從數據庫條目的問題我已經迴應我database table到我的網站,並勾選旁邊,在那裏你可以tick它和delete任何從網站上的條目,應該從刪除的表選項數據庫。由於某種原因,它不工作,我是一個初學者,任何幫助將不勝感激謝謝。有關於我的網站

<form method="" action="tester.php"> 
     <?php 

    include 'connect_to_mysql.php'; 

    $count=mysql_num_rows($result); 
    $result = mysql_query("SELECT * FROM booking ORDER BY ID ASC"); 

    echo "<table border='1'> 
    <tr> 
    <th>DEL</th> 
    <th>Name</th> 
    <th>Email ID</th> 
    <th>Phone Number</th> 
    <th>Collection Address</th> 
    <th>Collection Date</th> 
    <th>Collection Time</th> 
    <th>Make & Model</th> 
    <th>Message</th> 

    </tr>"; 

    while($row = mysql_fetch_array($result)) 
     { 
      ?> 
       <td align="center" bgcolor="#FFFFFF"><input name="delete_these[]"                               type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td> 
     <?php 
    echo "<td>" . $row['name'] . "</td>"; 
    echo "<td>" . $row['email'] . "</td>"; 
     echo "<td>" . $row['phonenumber'] . "</td>"; 
     echo "<td>" . $row['collectionaddress'] . "</td>"; 
     echo "<td>" . $row['collectiondate'] . "</td>"; 
     echo "<td>" . $row['collectiontime'] . "</td>"; 
     echo "<td>" . $row['makemodel'] . "</td>"; 
     echo "<td>" . $row['message'] . "</td>"; 
     echo "</tr>"; 
     } 
    echo "</table>"; 
    ?> <br> 
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit"     id="delete" value="Delete"></td> 
    </tr> 

    <?php 
    // Check if delete button active, start this 
    if(isset($_GET['delete'])) { 

    for($i=0;$i<$count;$i++){ 
    $id = $checkbox[$i]; 
    print_r($_GET['delete_these']); 
    $ids = implode(', ', $_POST['delete_these']); 
    $sql = "DELETE FROM booking WHERE id IN($ids)"; 
    echo "<br />SQL: $sql<br />"; 
    $result = mysql_query($sql); 
    } 

    // if successful redirect to delete_multiple.php 
    if($result){ 

    } 
    } 
    mysql_close(); 
    ?> 
    </form> 

回答

0

更換你的表單標籤:

<form method="post" action="tester.php"> 

那麼這3:

if(isset($_POST['delete'])) 

$id =(int)$_POST['delete_these'][$i]; 

$sql = "DELETE FROM booking WHERE id=$id"; 

在這3個地點:

if(isset($_GET['delete'])) 

$id = $checkbox[$i]; 

$sql = "DELETE FROM booking WHERE id IN($ids)"; 

$id = $checkbox[$i];作品只有當register_globals指令處於打開狀態,因爲它導致的安全問題,現在已被棄用(並且自PHP 5.4起刪除),並且如果它存在於PHP版本中,則您正在使用大多數託管服務將其關閉。

您將通過信息發送數據,以便您通過$_POST訪問它們。 $_POST之前的(int)將任何東西都轉換爲整數,所以如果用戶試圖發送一個字符串,它將變成0,這將保護你免受sql注入。

SIDE注意您應該停止使用mysql_ *函數,因爲它們已被棄用。退房PDOmysqli

+0

@ user3554323更新我的答案,你在你的查詢的問題也是如此,你在for循環中使用它,一次得到1個複選框,但是你正在刪除,就好像你已經崩潰了字符串 – CodeBird

0

你在你的表單中添加方法tag--

<form method="post" action="tester.php" > 

,並同時更換if(isset($_GET['delete']))if(isset($_POST['delete']))

+0

感謝幫助球員,我做了更改,但是沒有任何東西從數據庫中刪除:( – user3554323