简体   繁体   English

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

[英]Button to delete row from table - PHP SQL

I have a web page that outputs data from my sql database. 我有一个网页,可从sql数据库输出数据。 Each row has a delete button that should delete the data in this particular row. 每行都有一个删除按钮,应该删除该特定行中的数据。 I am having a issue where when I click the delete button it always deletes the row with the first/lowest ID regardless of which button I click. 我遇到一个问题,当我单击删除按钮时,无论单击哪个按钮,它总是删除具有第一个/最低ID的行。 It doesnt delete the row I want it to delete. 它不会删除我要删除的行。 Here is my code: HTML 这是我的代码: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 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);
    }
}

please could someone help me. 请有人可以帮我。 Thanks 谢谢

Because you delete the value of last id in 因为您在中删除了最后一个ID的值

$id = $_POST['id'];

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

the value of $_POST['id'] is equal to last row in <?php echo $row['id']?> in each run of the while the $_POST['id'] value replaced by new $row['id'] so the last on would be read in $_POST['id'] $_POST['id']值等于<?php echo $row['id']?>中的最后一行,而$_POST['id']值替换为新的$row['id']因此最后一个将在$_POST['id']读取

As you are using while($row = mysqli_fetch_assoc($result)) , this will loop to the last row in your MySQL Table, thus every time, it will have the last id of your table. 当您使用while($row = mysqli_fetch_assoc($result)) ,它将循环到MySQL表的最后一行,因此每次都会具有表的最后一个id

You can code it in a way that your delete script will get the id . 您可以通过某种方式对它进行编码,以使删除脚本获得id So, your delete button for each row will be have the row id , eg process.php?1 , process.php?2 , process.php?3 and so on. 因此,每行的删除按钮将具有row id ,例如process.php?1process.php?2process.php?3等。

tablepage.php (not sure of your page's real name) tablepage.php (不确定页面的真实名称)

Replace this line: 替换此行:

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

With this: 有了这个:

<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);
}

Hope that helps, thanks! 希望对您有所帮助,谢谢!

In actual in this condition ,use ajax and jquery is much more easier i think .But do only by php gonna be use logic code 实际上,在这种情况下,我认为使用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 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
    .....
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM