该行在MySQL中没有得到DELETED,PHP没有返回任何错误

问题描述 投票:0回答:2

我有一个代码,它应该通过加载带有SQL查询的remove.php文件从表中删除一个选定的ID行。问题是它不起作用但我没有错误或通知。我的变量似乎被转移到URL中的remove.php就好了,因为'<a href="remove.php?='.$Fvalue.'">'被解释为

remove.phpα= 1

例如,其中1是行的主要ID。

以下是与该问题相关的所有代码:

编辑:

使用REMOVE按钮为产品表呈现HTML:

    <!-- Products table -->

    <table class="table">
        <thead>
        <tr>
            <th scope='col'>id </br ></th><th scope='col'>name </br ></th><th scope='col'>price </br ></th><th scope='col'>amount </br ></th><th scope='col'>category_name </br ></th>    </tr>
        </thead>
        <tbody>

        <tr><td data-id="1"><a href="remove.php?remove_id=1"> REMOVE</a></td>
<td>iPhone 7</td>
<td>800</td>
<td>15</td>
<td>Smartphones</td>
</tr><tr>
<td data-id="42"><a href="remove.php?remove_id=42"> REMOVE</a></td>
<td>Motorola </td>
<td>3000</td>
<td>5</td>
<td>Smartphones</td>
</tr><tr><td data-id="2"><a href="remove.php?remove_id=2"> REMOVE</a></td><td>Macbook Pro 2015</td>
<td>1300</td><td>10</td>
<td>Computers</td></tr><tr><td data-id="4"><a href="remove.php?remove_id=4"> REMOVE</a></td>
<td>Dell XPS</td>
<td>1400</td>
<td>6</td><td>Computers</td>
</tr><tr><td data-id="41"><a href="remove.php?remove_id=41"> REMOVE</a></td>
<td>CHROMEBOOK</td>
<td>5600</td>
<td>8</td>
<td>Computers</td></tr></tbody>

更新的PHP:

 <?php
    foreach ($newArray as $value) {
        echo '<tr>';
        foreach ($value as $key => $Fvalue) {

            $remove = $value['id'] = " REMOVE";
            if($value[$key] == $value['id']) {
                echo '<td data-id="'.$Fvalue.'">' . '<a href="remove.php?remove_id='.$Fvalue.'">' . $remove . '</a>' . '</td>';  // will show all values.
            } else {
                echo '<td>' . $Fvalue . '</td>';
            }
        }
        echo '</tr>';
    }
    ?>

remove.php

<?php
require_once ("navigation.php");
require_once("database_connection.php");
$id = !empty($_GET['remove_id']) ? $_GET['remove_id'] : null;
if($id != null) {
    $deleteProducts = "DELETE FROM `products` WHERE `id` = '.$id.'";
    mysqli_query($dbc, $deleteProducts);
}

寻找任何帮助我发现任何问题,因为我没有错误,也不知道为什么代码没有删除表中的行。谢谢。

php mysql sql sql-delete
2个回答
3
投票

您必须传递与您发送的值相关联的密钥;

<a href="remove.php?remove_id='.$Fvalue.'">' . $remove . '</a>

注意remove_id部分。

现在,在PHP中,您可以检索此值;

$id = !empty($_GET['remove_id']) ? $_GET['remove_id'] : null;
if($id !== null) {
    $deleteProducts = "DELETE FROM `products` WHERE `id`='{$id}'";
    mysqli_query($dbc, $deleteProducts);
}

在我的代码中,我还修复了代码中的问题。你有$id = (isset($_GET['$Fvalue']));,它总是将$id设置为truefalse,而不是你已经通过的ID。

您还可以使用双引号而不是单引号来清理HTML变量。

if($value[$key] == $value['id']) {
    echo "<td data-id='{$Fvalue}'><a href='remove.php?remove_id={$Fvalue}'>{$remove}</a></td>";  // will show all values.
} else {
    echo "<td>{$Fvalue}</td>";
}

大注:Little Bobbyyou may be at risk for SQL Injection Attacks。通过Prepared Statements了解parameterized queries


0
投票

使用get方法,您需要在网址中使用键值对。

remove.php?id=1

其中id是键,1是值。

© www.soinside.com 2019 - 2024. All rights reserved.