简体   繁体   English

表格行删除按钮

[英]Table row delete button

When I try the delete button I get no errors and the page refreshes but the data won't be deleted, so I think the problem is in passing along the id from a row. 当我尝试删除按钮时,没有错误,页面刷新,但是数据不会被删除,所以我认为问题在于从行中传递ID。

None of the other solutions have worked for me so far. 到目前为止,没有其他解决方案对我有用。 This is the table body. 这是桌子的身体。

<tbody>
<?php

$server = "localhost";
$user = "Website";
$pass = "pass";
$db = "db";

$conn = new mysqli($server, $user, $pass, $db);

if ($conn->connect_error)
{
    die("connection to database failed");
}

$query = "SELECT * FROM koppeling";
$result = mysqli_query($conn, $query);

while($row = $result-> fetch_assoc())
{
    echo "<tr>";
    echo "<td>".$row['Wagon_ID']."</td>";
    echo "<td name='wagon'>".$row['EPC']."
    <a href='delete.php' class='table-button' 
    id='".$row['id']."'>Delete</a>";
    echo "<td>";
    echo "</td>";
    echo "</tr>"; 
}
    ?>
</tbody>

delete.php file: delete.php文件:

<?php
$id = $_GET['id'];
$server = "localhost";
$user = "Website";
$pass = "pass";
$db = "db";

$conn = new mysqli($server, $user, $pass, $db);

if ($conn->connect_error)
{
    echo "Conn db failed";``
}

$query = "DELETE FROM koppeling WHERE id='$id';";

try
{
    mysqli_query($conn, $query);
    mysqli_close($conn);
    header('Location: koppelen.php');
}

catch (Exception $e)
{
    echo "$e";
}
?>

Can anyone help me figure out what is wrong, I've been stuck on this quite a while now. 谁能帮助我找出问题所在,但我已经坚持了很长时间。

Instead if this 相反,如果这

while($row = $result-> fetch_assoc())
{
    echo "<tr>";
    echo "<td>".$row['Wagon_ID']."</td>";
    echo "<td name='wagon'>".$row['EPC']."
    <a href='delete.php' class='table-button' 
    id='".$row['id']."'>Delete</a>";
    echo "<td>";
    echo "</td>";
    echo "</tr>"; 
}

Write this 写这个

while($row = $result-> fetch_assoc())
{
    echo "<tr>";
    echo "<td>".$row['Wagon_ID']."</td>";
    echo "<td name='wagon'>".$row['EPC']."
    <a href='delete.php?id=".$row['id']."' class='table-button' 
    id='".$row['id']."'>Delete</a>";
    echo "<td>";
    echo "</td>";
    echo "</tr>"; 
}

It would be better if you used form with POST request or ajax instead of links. 如果将表单与POST请求或Ajax一起使用,而不是使用链接,那会更好。

If you really want to do it this way you can add query string to your href attribute. 如果您确实想用这种方法,可以将查询字符串添加到href属性。

$url = "delete.php?id=" . $row['id'];
<a href="<?php echo $url ?>" class='table-button' 
id='".."'>Delete</a>";

You have to change the below line 您必须更改以下行

$query = "DELETE FROM koppeling WHERE id='$id';"; $ query =“从koppeling删除,其中id ='$ id';”;

to

$query = "DELETE FROM koppeling WHERE id='".$id."';"; $ query =“从koppeling删除,其中id ='”。$ id。“';”;

so that id value will be properly populated in the query 以便在查询中正确填充ID值

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

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