在while循环中使用PHP MySQL Delete函数

问题描述:

晚上

我有点卡在这里.我已经为我们的员工进行了登录,可以轻松创建新发票(虽然抱歉,它是丹麦的),但几乎所有工作都已完成.但是我不知道怎么做的一件事是从while循环中删除一个材料(丹麦语中的materiale).我有一个带有while循环的表单,其中显示了所有材料,在材料输入字段的旁边,我创建了一个小href链接,该链接应在javascript的帮助下删除该字段.它会完美删除该字段,但不会从我们的mysql数据库中删除数据.因此,每次刷新页面时,数据都会返回.更新和插入不是问题.代码如下:

I'm kinda stuck here. I've made a login for our employees to easily create new invoice (it's in danish though, sorry) and almost everything is completed. But one thing I can't figure out how to do, is to delete a material (materiale in danish) from a while loop. I've got a form with a while loop within it, which displays all of the materials, and right next to the material input fields I've created a small a href link, which should delete the field with some help from javascript. It deletes the field perfectly, but doesn't delete the data from our mysql database. So every time I refresh the page, then the data are back. Updating and inserting isn't a problem. The code looks like this:

$ct_update = 0;

foreach ($_POST['beskrivelse_update'] as $k => $value) {
    $beskrivelse_update = addslashes($value);
    $enhed_update           = addslashes($_POST['enhed_update'][$ct_update]);
    $stk_update         = addslashes($_POST['stk_update'][$ct_update]);
    $vejl_eks_moms_update   = addslashes($_POST['vejl_eks_moms_update'][$ct_update]);

    $update_id              = (int)$_POST['update_id'][$ct_update];

    $db->query("UPDATE faktura_materialer SET beskrivelse = '$beskrivelse_update', enhed = '$enhed_update', stk = '$stk_update', vejl_eks_moms = '$vejl_eks_moms_update', subtotal = '$stk_update' * '$vejl_eks_moms_update' WHERE id = '$update_id' LIMIT 1");

    $ct_update++;
}

$ct_ny = 0;

if(isset($_POST['beskrivelse_ny'])){
    foreach ($_POST['beskrivelse_ny'] as $k => $value) {
        $beskrivelse_ny = addslashes($value);
        $enhed_ny           = addslashes($_POST['enhed_ny'][$ct_ny]);
        $stk_ny         = addslashes($_POST['stk_ny'][$ct_ny]);
        $vejl_eks_moms_ny   = addslashes($_POST['vejl_eks_moms_ny'][$ct_ny]);

        $db->query("INSERT INTO faktura_materialer (faktura_id, beskrivelse, enhed, stk, vejl_eks_moms, subtotal) VALUES ('".$_faktura->faktura_id."', '".$beskrivelse_ny."', '".$enhed_ny."', '".$stk_ny."', '".$vejl_eks_moms_ny."', '".$stk_ny."' * '".$vejl_eks_moms_ny."')");

        $ct_ny++;
    }
}

希望您能理解我的意思,否则请发表评论.晚上有个愉快的窥视:)

Hopefully you understand what I want to do, otherwise just comment. Have a nice evening peeps :)

我希望在用户单击提交按钮保存更改"时提交删除查询.这是我的JavaScript代码:

I want the delete query to be submitted when the user clicks the submit button "Save changes". This is my javascript code:

<script type="text/javascript">
function myFunction()
{
var table = document.getElementById("produkter_rows");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = '<td><input style="width:450px;" class="text-input" type="text" name="beskrivelse_ny[]" value=""></td>';
cell2.innerHTML = '<td><input style="width:60px;" class="text-input" type="text" name="enhed_ny[]" value=""></td>';
cell3.innerHTML = '<td><input style="width:30px;" class="text-input" type="text" name="stk_ny[]" value=""></td>';
cell4.innerHTML = '<td><input style="width:205px;" class="text-input" type="text" name="vejl_eks_moms_ny[]" value=""></td>';
cell5.innerHTML = '<td><a href="#" onclick="removeRow(this)" id="addNew" title="Slet produkt"><img src="images/icons/slet.gif" width="16" alt="Slet" /></a></td>';
}

removeRow = function(el) {
    $(el).parents("tr").remove()       
}
</script>

当用户单击材料旁边的删除图标时,它将被删除.而且确实会删除输入字段,但不会从mysql的数据库中删除.

When the user clicks the delete icon right next to the material, then it will be deleted. And it does delete the input fields, but not from the database in mysql.

第二次修改:

这是HTML和PHP,用于更新MySQL数据库中的当前数据:

This is HTML and PHP for updating current data in MySQL database:

$materialer_query = $db->query("SELECT faktura_materialer.id as materialer_id, faktura.*, faktura_materialer.* FROM faktura, faktura_materialer WHERE faktura_materialer.faktura_id = faktura.id");

while($mat = $materialer_query->fetch_object()){
    ?>

    <tr>
        <input type="hidden" name="update_id[]" value="<? print $mat->id; ?>" />
        <td><input style="width:450px;" class="text-input" type="text" name="beskrivelse_update[]" value="<? print $mat->beskrivelse; ?>"></td>
        <td><input style="width:60px;" class="text-input" type="text" name="enhed_update[]" value="<? print $mat->enhed; ?>"></td>
        <td><input style="width:30px;" class="text-input" type="text" name="stk_update[]" value="<? print $mat->stk; ?>"></td>
        <td><input style="width:205px;" class="text-input" type="text" name="vejl_eks_moms_update[]" value="<? print $mat->vejl_eks_moms; ?>"></td>
        <td></td>
        <td><a href="#" onclick="removeRow(this)" id="addNew" title="Slet produkt"><img src="images/icons/slet.gif" width="16" alt="Slet" /></a></td>
    </tr>

    <?
}

当我更新时,隐藏的输入字段name="update_id[]"被添加到表单中.我希望这会有所帮助...

The hidden input field name="update_id[]" is added to the form when I am updating. I hope this helps...

您需要通知PHP脚本您希望删除记录.一种解决方案是使用隐藏的输入,例如在调用removeRow时设置的

You need to inform the PHP script that you wish to delete the record. One solution would be to use a hidden input which is set when you call removeRow, e.g.

function removeRow(el) {
    // get the table row
    var row = $(el).parents('tr:first');

    // disable the input fields for the row
    $('input', row).attr('disabled', 'disabled');

    // rename update_id to delete_id and re-enable the field
    $('input[name="update_id[]"]', row).attr('name', 'delete_id[]').removeAttr('disabled');

    // hide the row
    row.hide();
}

此方法不再从DOM中删除表行.但是,它将禁用行中包含的输入字段,并添加名称为beskrivelse_delete的隐藏输入.

This method no longer removes the table row from the DOM. However, it will disable the input fields contained in the row and adds a hidden input with the name beskrivelse_delete.

提交表单后,您的PHP现在可以知道需要删除哪些记录.

When the form is submitted, your PHP will now have a way to know which records need to be deleted.

foreach ($_POST['delete_id'] as $deleteId) {
    $deleteId = (int)$deleteId;
    $db->query("DELETE FROM faktura_materialer WHERE id = $deleteId");
}


更新:我创建了一个 phpfiddle ,您可能希望对其进行一些改进学习关于.这些要点包括:


Update: I've created a phpfiddle which has some improvements that you may wish to learn about. These points include:

  • 在未启用JavaScript的情况下支持删除
  • 使用准备的语句(首选add_slashes)
  • 使用 htmlspecialchars
  • 转义输出
  • 基于增量计数器明确定义输入ID,以避免猜测
  • Supporting deletion where JavaScript is not enabled
  • Using prepared statements (preferred to add_slashes)
  • Escaping output by using htmlspecialchars
  • Explicitly defining input ids to avoid guessing based on an incremental counter