按下按钮不起作用时删除行

时间:2019-01-31 23:58:31

标签: php html database mysqli

因此,目前有160台服务器从数据库中拉出并相互堆叠: <tr> <td> 该行的最后一个<td>应该触发从数据库中删除该特定行,但是不会,并且此时将我链接到错误页面。

主要代码:

<?php

require_once "config/config.php";

$sql = "SELECT * FROM deployments";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['server'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['url'] . "</td>";
echo "<td>" . $row['port'] . "</td>";
echo "<td><span class='badge badge-warning'>ERROR</span></td>";
echo "<td><a href='config/delete.php?id=". $row['server'] ."' title='Delete Record' data-toggle='tooltip'><span class='fa fa-trash'></span></a></td>";
echo "</tr>";
}
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

?>

Delete.php页面代码:

<?php
// Process delete operation after confirmation
if(isset($_POST["server"]) && !empty($_POST["server"])){
    // Include config file
    require_once "config/config.php";

    // Prepare a delete statement
    $sql = "DELETE FROM deployments WHERE server = ?";

    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_server);

        // Set parameters
        $param_server = trim($_POST["server"]);

        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Records deleted successfully. Redirect to landing page
            header("location: ../deployments.php");
            exit();
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);

    // Close connection
    mysqli_close($link);
} else{
    // Check existence of id parameter
    if(empty(trim($_GET["server"]))){
        // URL doesn't contain id parameter. Redirect to error page
        header("location: error.php");
        exit();
    }
}
?>

在单击fa fa-trash图标时,应从数据库中删除与server name url port共享的图标行。

3 个答案:

答案 0 :(得分:0)

您应该使用$ _GET ['id']或$ _REQUEST ['id']代替$ _POST [“ server”]

用下面的代码替换您的Delete.php代码

    <?php
// Process delete operation after confirmation
if (isset($_GET["id"]) && !empty($_GET["id"])) {
    // Include config file
    require_once "config/config.php";

    // Prepare a delete statement
    $sql = "DELETE FROM deployments WHERE server = ?";

    if ($stmt = mysqli_prepare($link, $sql)) {

        // Set parameters
        $param_server = trim($_GET["id"]);

        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_server);

        // Attempt to execute the prepared statement
        if (mysqli_stmt_execute($stmt)) {
            // Records deleted successfully. Redirect to landing page
            header("location: ../deployments.php");
            exit();
        } else {
            echo "Oops! Something went wrong. Please try again later.";
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);

    // Close connection
    mysqli_close($link);
} else {
    // Check existence of id parameter
    if (empty(trim($_GET["id"]))) {
        // URL doesn't contain id parameter. Redirect to error page
        header("location: error.php");
        exit();
    }
}
?>

答案 1 :(得分:0)

我本人已对代码进行了一些更改,感谢所有帮助,删除仍然无法正常进行,我一直从error.php页面收到错误消息。

<?php
// Process delete operation after confirmation
if (isset($_GET["id"]) && !empty($_GET["id"])) {

$id = $_POST["id"];

// Include config file
require_once "config/config.php";

// Prepare a delete statement
$sql = "DELETE FROM deployments WHERE id = '$id'";

if ($stmt = mysqli_prepare($link, $sql)) {

// Set parameters
$param_id = trim($_GET["id"]);

// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_id);

// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
// Records deleted successfully. Redirect to landing page
header("location: ../deployments.php");
exit();
} else {
echo "Oops! Something went wrong. Please try again later.";
}
}

// Close statement
mysqli_stmt_close($stmt);

// Close connection
mysqli_close($link);
} else {
// Check existence of id parameter
if (empty(trim($_GET["id"]))) {
// URL doesn't contain id parameter. Redirect to error page
header("location: error.php");
exit();
}
}
?>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="alert alert-danger fade in">
<input type="hidden" name="id" value="<?php echo trim($_GET["id"]); ?>"/>
<p>Are you sure you want to delete this record?</p><br>
<p>
<input type="submit" value="Yes" class="btn btn-danger">
<a href="../dashboard.php" class="btn btn-default">No</a>
</p>
</div>
</form>

答案 2 :(得分:-1)

将代码更改为...

// Process delete operation after confirmation

if(isset($_POST["id"]) && !empty($_POST["id"])) {

  $id = $_POST["id"];

  //Include config file

  require_once("config/config.php");

  //Prepare a delete statement

  $sql = "DELETE FROM deployments WHERE server = '$id' ";
相关问题