2015-04-06 129 views
0

我正在一個網站上工作,我卡住了。我的網站在我的數據庫中顯示數據。表中最後一列是複選框。如果我「檢查」複選框,我想刪除數據庫中的複選框記錄。任何援助之手都會有所幫助。複選框的記錄必須從數據庫中刪除

如果我按刪除按鈕,我想刪除主題哪一行是複選框。

這裏的源代碼:

echo '<table align="center" border="1" style="width:50% "><tr><th>Fórum neve</th><th>Tulajdonos</th><th>Létrehozás ideje</th><th>Hozzászólások száma</th><th>Törlés</th></tr>'; 

$sql = "SELECT topicname, username, created, COUNT(commentid) 
     FROM user,topic,comment 
     WHERE topic.topicid = comment.whichtopic 
     AND user.userid = topic.owner 
     GROUP BY topicname"; 
$result = mysql_query($sql); 
while($row = mysql_fetch_array($result)) { 
    echo '<tr><th>' . $row["topicname"] . '</th><td>' . $row["username"] . '</td><td>'. $row["created"] .'</td><td>'. $row["COUNT(commentid)"] .'</td> 
       <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row["topicname"]; ?>></td></tr>'; 
} 
echo '</table>'; 
echo '<form name="form1" method="post" action=""> 
     <input name="delete" type="submit" id="delete" value="Delete"> 
     </form>'; 
if($delete){ 
    for($i=0;$i<$count;$i++){ 
     $del_id = $checkbox[$i]; 
     $sql = "DELETE FROM topic WHERE topicname='$del_id'"; 
     $result = mysql_query($sql); 
    } 
} 
+0

其中$算什麼? – 2015-04-06 13:15:53

+0

我知道我們只是在看你的代碼的一個片段,但是Vicky指出$ count是未定義的,就像$ delete一樣,我沒有看到你提交的操作。 – BigScar 2015-04-06 13:19:20

+0

而你的表單標籤應包含你的複選框。 – BigScar 2015-04-06 13:27:31

回答

0

你可能在錯誤的道路

topicname='$del_id' 

打印的​​執行DELETE查詢之前被刪除。我認爲topicname沒有找到任何匹配項。試試這個

$sql = "DELETE FROM topic WHERE topicid ='$del_id'"; 
0

請試試這個

if(isset($_POST['delete'])){ 
     for($i=0;$i<count($_POST['checkbox']);$i++){ 
      $del_id = $_POST['checkbox'][$i]; 
      $sql = "DELETE FROM topic WHERE topicname='$del_id'"; 
      $result = mysql_query($sql); 
     } 
    } 
+0

還留着複選框 – 2015-04-06 13:31:49

+0

發生什麼 表單標籤中顯示此錯誤消息: 「通知:未定義指數:複選框在C:\ XAMPP \ htdocs中\ ASD \ topicedit.php上線84」哪一個是這條線: $ del_id = $ _POST ['checkbox'] [$ i]; – david 2015-04-06 13:32:54

相關問題