down100009 2014-04-13 17:45
浏览 20
已采纳

我只能使用复选框从数据库中删除最后一行

i have one html table with all the rows value from a specific database table.

I created a html table column to eliminate a specific row from the database, inside this column exists a checkbox to check and eliminate multiple table rows from the database.

But i'm having some problems, i can only delete the last row, example: Lets imagine that you check the second row, and you click at submit button and nothing happens.

But if you check the last inserted row, and you click submit, it deletes the row from the database.

I just can delete the last row, i can't delete multiple rows by checking a specific number of checkboxes.

I did this:

example

I'm using the MVC structure:

View:

<form method='POST'>
<?php
echo "<td><input style='width: 50px;' class='delete' value='delete'  type='submit' name='del[]' /></td>";
    foreach($editjoarray as $key){    
    echo "
    <table>
    <tr>
      <td> ".$key['id']."</td>

      <td><input type='checkbox' name='delete[]' value=".$key['id']." /></td>
    </tr>
    </table>
    </form>";

    }

Model:

function fname(){

if(isset($_POST['del']) && !empty($_POST['delete'])) {
{
    $checkbox = $_POST['delete'];
    $countCheck = count($_POST['delete']);

for($i=0;$i<$countCheck;$i++) {
$del_id = $checkbox[$i];

echo $del_id;
  $sql = "DELETE FROM mytable WHERE id=$del_id";
   $exe = mysql_query($sql) or die(mysql_error());
}
}
}


   printFunction();
}
  • 写回答

1条回答 默认 最新

  • douyong5476 2014-04-13 17:50
    关注

    You are closing the form tag inside the loop so there will always be only one delete checkbox (as post values). You need to move the closing tag of the form </form> outside the loop so that all the checkboxes lies in the same form.

    <form method='POST'>
    <?php foreach($editjoarray as $key){    
       /*
       delete checkboxes
       input fields
       */
    
    } ?>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?