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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line