doumu6941 2013-03-26 17:34
浏览 29

mysqli查询删除

I have all data from a database table display as an HTML table on a page. When an admin selects a row, it adds a class of 'remove' to the parent row in the HTML table.

I then want to delete it from the mysqli database. Would I set the row fields as variables and delete the row that matches it in the database? I'm not sure how to do it, could someone please point me in the right direction?

HTML

<table>
  <tr class="remove">
    <td>field1</td>
    <td>field2</td>
    <td>field3</td>
  </tr>
  <tr>
    <td>another field</td>
    <td>another field</td>
    <td>another field</td>
  </tr>
</table>

Mysqli (relevant sample)

  $mysqli->query("DELETE FROM $table WHERE Date="??" LIMIT 1");
  • 写回答

1条回答 默认 最新

  • dongnai3960 2013-03-26 23:01
    关注

    If you're using JavaScript, which I assume you are as you're adding a class to a row when it's selected, you could do the following:

    1. Include a hidden disabled input in each row:

    <tr class="remove">
        <td>field1</td>
        <td>field2</td>
        <td>
    
            field3
    
            <input type="hidden" disabled name="delete[]" value="<?php echo $id; ?>">
    
        </td>
    </tr>
    

    2. Enable the hidden input when it is selected to be removed:

    I'm not going to guess how you're adding the remove class to the row, but when doing so have it remove the disabled attribute of the hidden delete[] input contained within it.

    3. Post the form:

    Once posted you can run the query to delete all the rows with an ID in the $_POST['delete'] array.

    If you're using prepared statements you can use the following query where ? is implode(',', $_POST['delete']):

    "DELETE FROM " . $table . " WHERE FIND_IN_SET(id, ?) != 0"
    

    I'm assuming here for the sake of simplicity that you have a unique id field (that doesn't contain ,s as part of its value). Obviously change to whatever is relevant for your database.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?