G
G
German Zuiakov2021-07-24 20:53:56
PHP
German Zuiakov, 2021-07-24 20:53:56

Gives an error when deleting via id?

It gives an error when trying to delete via id, while it redirects to erorr.php, tried it via action, everything works, but it doesn’t work, because there may be the same

delete.php tasks:

<?php
require_once "db.php";
// Process delete operation after confirmation
if(isset($_POST["id"]) && !empty($_POST["id"])){
    // Prepare a delete statement
    $sql = "DELETE FROM excel WHERE id = ?";
    
    if($stmt = mysqli_prepare($con, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_id);
        
        // Set parameters
        $param_id = trim($_POST["id"]);
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Records deleted successfully. Redirect to landing page
            header("location: index.php");
            exit();
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
    
    // Close connection
    mysqli_close($con);
} else{
    // Check existence of id parameter
    /*if(empty($_GET["id"])){
        // URL doesn't contain id parameter. Redirect to error page
        header("location: error.php");
        exit();
    }*/
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Delete Record</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        .wrapper{
            width: 600px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <h2 class="mt-5 mb-3">Delete Record</h2>
                    <form id="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                        <div class="alert alert-danger">
                            <input type="hidden" name="id" value="<?php echo trim($_POST["id"]); ?>">
                            <p>Are you sure you want to delete this employee record?</p>
                            <p>
                                <input type="submit" value="Yes" class="btn btn-danger">
                                <a href="index.php" class="btn btn-secondary">No</a>
                            </p>
                        </div>
                    </form>
                </div>
            </div>        
        </div>
    </div>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2021-07-25
@HermanZuiakov

First of all -

<input type="hidden" name="id" value="<?php echo trim($_POST["id"]); ?>">
- I know that you have an eternally empty value there, so the form sends absolutely nothing. Well, the code accordingly sends you to ... error.php.
Secondly
<form id="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
this construction shows us how deep the insanity has gone in not studying the basic things, besides the fact that this is complete nonsense that does not carry any meaning, it’s also an idea, apparently - to send it to yourself is done with a C with a minus, since in this case the action can be omitted at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question