2015-04-06 134 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

相关问题