MySQL Delete

Erasing Data with Surgical Precision

The last of the operations is the delete operation. Just like with our update functionality, there is no delete method. We can submit a hidden method with delete as the value. We can use post and specify a value, but we’re building up for Laravel. Laravel accepts various requests and can process them easily. Here’s a list from their documentation.

https://laravel.com/docs/9.x/controllers#actions-handled-by-resource-controller

As you can see, if we wanted to delete a photo, we need to provide the {photo} resource. In our case, we’ll need to provide the author’s id.

Previous Code Review

Make sure that you’re up to date on the previous code.

The index.php file contains our route. We’ll add a new route for our delete functionality. We’ll also create a new file called, DeleteAuthor.php that will call the Author::delete method.

Outlining the Deletion Plan

With our update functionality, we didn’t allow the user to select the author to update; we hardcoded the id so that the user can only update the author with id = 1. With the deletion, we want to do this a little differently.

  • Get all of the authors
  • Display a list of the authors and have a delete link next to the author’s details.
  • Once the delete link is clicked, the information is directed to the index.php route file, which then calls the Author::delete method.
  • Upon deletion, the user is redirected back to the delete page.

The only thing that we’re missing to make this a true MVCish framework, is the controller. Our route file is acting like the controller. We’re not going to cover that here, but just a fun fact for you.

Get all of the authors

We created our select_all method that retrieves all of the authors. Pretty straightforward.

<?php
require_once("./Author.php");

use Authors\Author;

$author = new Author;
$authors_to_delete = $author->select_all();

Display all of the authors

Now that we have all of the authors, we need to display them. We can do this a couple of different ways:

  • Display a link that allows the user to delete the author. This uses a get request.
  • Use a form to delete the author. We’re going to use this since we can submit our simulated delete request.
<?php
require_once("./Author.php");

use Authors\Author;

$author = new Author;
$authors_to_delete = $author->select_all();

foreach( $authors_to_delete as $author_to_delete )
{
    ?>
    <form action="index.php?page=author" method="post">
        <input type="hidden" name="_method" value="delete">
        <input type="hidden" name="id" value="<?php echo $author_to_delete['id']; ?>">
        <div>
            <?php
            echo $author_to_delete['first_name'] . " "
                . $author_to_delete['last_name'] . " "
                . $author_to_delete['email'];
            ?>

            <input type="submit" value="Delete">
        </div>
    </form>
    <?php
}

We loop through each of our authors and wrap them in a form. The _method value is set to delete. We have a hidden id field that we’re going to submit when we press the Delete button.

Adding a route to the index.php file

I’ve removed the put and patch requests from our index.php file so that we can only focus on the delete request.

<?php
require_once("./Author.php");

use Authors\Author;

if ( !isset($_GET['page']) ) {
    ?>
    <a href="DeleteAuthor.php">Delete Author</a>
    <?php
    die();
}

$page   = $_GET['page'];
$method = $_POST['_method'];

if ( $page == "author" ) {
    if ( $method == "delete" ) {
        $author = new Author;
        $delete_success = $author->delete($_POST);

        if ( $delete_success ) {
            header("Location: DeleteAuthor.php");
            die();
        } else {
            die("Could not delete");
        }
    }
}

The script will check to see if the delete request is passed. If it is, it’s going to send the $_POST to our delete method. We haven’t created our delete method, but will do that next. It’s going to return either true or falsetrue if the deletion was successful.

Adding the delete method to Author

The final piece to this puzzle is to actually remove the resource from the authors table. We’ll use prepared statements again.

The sql is prepared and the id is bound to the script before it’s executed. If there are no errors, true is returned. There are no changes to the DB class.

And we’re done. In the next article, we’ll combine all of our MySQL scripts to create one large resource file. We’ll be able to:

  • See all authors
  • See a single author
  • Update a specific author
  • Add a new author
  • Delete an author

https://github.com/dinocajic/php-youtube-tutorials?source=post_page—–ec6f3a969591——————————–

MySQL Update

REVAMPING DATA WITH PRECISION

PHP – P89: mysql update

The next logical step in our MySQL progression is updating an existing resource. We’re going to introduce the differences between PUT and PATCH and put it to rest once and for all.

MySQL Delete

Erasing Data with Surgical Precision

PHP – P90: mysql delete

The last MySQL topic is delete. Just like with our update functionality, there is no delete method. We can submit a hidden method with delete as the value.

MySQL CRUD

FROM CREATION TO DELETION: MYSQL’S COMPLETE DATA TOOLKIT

PHP – P91: mysql crud

What are CRUD operations? Create, Read, Update, Delete. The last piece of the MySQL puzzle is to combine those operations into one file.

Leave a Reply