从表中删除行的按钮 - PHP SQL

时间:2016-02-20 01:07:16

标签: php mysql

我有一个从我的sql数据库输出数据的网页。每行都有一个删除按钮,该按钮应删除此特定行中的数据。我遇到一个问题,当我单击删除按钮时,无论我单击哪个按钮,它总是删除具有第一个/最低ID的行。它不会删除我想要删除的行。这是我的代码: HTML

<form action="process.php" method="post">
<?php
$sql = "
    SELECT *
      FROM playerTeam
";
$result = mysqli_query($connection, $sql);
?>      
<table>
    <?php
    if (mysqli_num_rows($result) > 0) {
         while ($row = mysqli_fetch_assoc($result)) { ?>
            <tr>
                <td><input type="text" name="id" value="<?php echo $row['id']?>"></td>
                <td><input type="text" name="fName[]" value="<?php echo $row['firstName']?>"></td>
                <td><input type="text" name="sName[]" value="<?php echo $row['surName']?>"></td>
                <td><input type="text" name="team[]" value="<?php echo $row['team']?>"></td>
                <td><input type="submit" name="delete" value="Delete"></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
            <?php
        }
    } 
    ?>
    </table>
</form>

process.php

if (isset($_POST['delete'])) {
    $id = $_POST['id'];
    $sql = "DELETE FROM playerTeam WHERE id='$id'";

    if (mysqli_query($connection, $sql)) {
        echo "Record deleted";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($connection);
    }
}

请有人帮助我。感谢

3 个答案:

答案 0 :(得分:0)

因为您删除了

中最后一个ID的值
$id = $_POST['id'];

$sql = "DELETE FROM playerTeam WHERE id='$id'";

$_POST['id']的值等于<?php echo $row['id']?>中的最后一行 在每次运行中,$_POST['id']值被新$row['id']替换,因此最后一次将在$_POST['id']中读取

答案 1 :(得分:0)

在使用while($row = mysqli_fetch_assoc($result))时,这将循环到MySQL表的最后一行,因此每次都会有你表的最后id

您可以使用删除脚本获取id的方式对其进行编码。因此,每行的删除按钮都将包含row id,例如process.php?1process.php?2process.php?3等等。

tablepage.php (不确定您网页的真实姓名)

替换此行:

<td><input type="submit" name="delete" value="Delete"></td>

有了这个:

<td><p><a href="/process.php?id=<?php echo $id; ?>"></td>

process.php?id=1

// this will get `id=1` and thus your `id` will be `1`, and it will delete row `1`.
$id = $_GET['id'];

$sql = "DELETE FROM playerTeam WHERE id='$id'";

if (mysqli_query($connection, $sql)) {
    echo "Record deleted";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}

希望有所帮助,谢谢!

答案 2 :(得分:0)

实际上在这种情况下,我认为使用ajax和jquery要容易得多。但只能通过php来使用逻辑代码

  $i = 0;
  while($row = mysqli_fetch_assoc($result)) 
     {
     ?>
        <tr>
            <td><input type="text" name="id_<?= $i ?>" value="<?php echo $row['id']?>"></td>
            <td><input type="text" name="fName[]" value="<?php echo $row['firstName']?>"></td>
            <td><input type="text" name="sName[]" value="<?php echo $row['surName']?>"></td>
            <td><input type="text" name="team[]" value="<?php echo $row['team']?>"></td>
            <td><input type="submit" name="delete[<?= $i ?>]" value="Delete"></td>
            <td><input type="submit" name="update[<?= $i ?>]" value="Update"></td>
        </tr>
        <?php
      $i++; //for unique num for each row
    }

process.php

if (isset($_POST['delete'])){
    $delete_id = array_keys($_POST['delete']);//print array key as a array
    $delete_id = $delete_id[0];//so get array key
    $id = $_POST["id_$delete_id"];//get its relative id
    .....
 }