Удалить строку при нажатии кнопки не работает - PullRequest
0 голосов
/ 01 февраля 2019

Таким образом, в настоящее время 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 голосов
/ 01 февраля 2019

Вы должны использовать $ _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();
    }
}
?>
0 голосов
/ 01 февраля 2019

Я сделал некоторые изменения в коде сам, спасибо за помощь, удаление все еще не работает, и я продолжаю получать сообщение об ошибке со страницы 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>
0 голосов
/ 01 февраля 2019

Измените свой код на ...

// 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' ";
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...