treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Deleting multiple rows in MySQL database using ajax and php

  • Hello there, I am working on a small CRM for a Racing Team. I am creating a page where they can write a post and delete the posts at their liking. I am using ajax to do all the data transmission in the background to prevent page refresh. The whole posting is done, working and easy. But I can't get my head around deleting a single or multiple posts.

    Here is the context:

    When the client sings in the admin.php page, I am generating a <form> which contain a <fieldset> per post title.

        <form name="post_list" id="post_list">
                <fieldset class="post_options">
                        <input type="button" name="new_post_button" id="new_post_button" value="New post">
                        <input type="submit" name="delete_post_button" id="delete_post_button" value="Delete post">
                 </fieldset>
    
                 <?php
                         $query = "SELECT * FROM news ORDER BY post_date DESC";
                         $test = mysqli_query( $dbc, $query )
                                 or die ('Could not query the information to News table.');
    
                         while ( $row = mysqli_fetch_array($test) ) {
                                 $id =  $row['id'];
                                 $postDate = $row['post_date'];
                                 $postMonth = date(M, strtotime($postDate));
                                 $postDay = date(j, strtotime($postDate));
                                 $title =  $row['post_title'];
    
                                 echo '<fieldset class="old_posts">';
                                     echo '<input type="checkbox" name="select_post[]" class="select_post" value="' . $id . '">';
                                     echo '<input type="text" name="old_post_title" class="old_post_title" value="' . $title . '">';
                                     echo '<span class="old_post_edit" title="' . $id . '"></span>';
                                     echo '<p class="old_post_date">' . $postMonth . '. ' . $postDay . '</p>';
                                 echo '</fieldset>';
                           }
                           mysqli_close( $dbc );
                   ?>
         </form>
    

    At the top of the code you can see I have a delete post button to trigger the ajax code to delete the single or multiple posts. How would I go about using JQuery AJAX to do a simple PHP:

    foreach ( $_POST['select_post'] as $postID )
    {
        $query = "DELETE FROM news WHERE id = $postID";
        mysqli_query( $dbc, $query )
            or die ( 'Could not delete the post.' );
    }
    

    Will doing this work? Is my data: { select_post : postID } automatically an array?

    $('#post_list').submit( function () {
        var postID = $('.select_post').val();
        $.ajax({
            type: 'POST',
            url: 'delete_post.php',
            data: { select_post : postID }
        }).success( function () {
                alert('deleted');
            });
    });
    
  • NEVERMIND!!! I just had to create an array and pass it to my delete.php file.

    $('#post_list').submit( function (e) {
        e.preventDefault();
    
        var postID = [];
            $(".select_post[name='select_post[]']:checked").each(function () {
                postID.push(parseInt($(this).val()));
            });
        alert(postID);
    
        $.ajax({
            type: 'POST',
            url: 'delete_post.php',
            data: { select_post : postID }
        }).success( function () {
                alert('deleted');
            });
    });
    

    Thanks to myself :P