why update and delete query not worked in custom table?

this is custom template. I wrote crud by using wordpress query to sent data in custom table of database and get from database, insert query worked, but update and delete query not working. Below I give update and delete query, please suggest if anyone knows?

data inserted but when refresh, data deleted by itself and not update or delete
this is insert query

<?php
/*
 * Template Name:Form Template page
 */
get_header(); ?>
  <?php
        global $wpdb;
        $table= 'wp_contact_form';
         if (isset($_POST['submit'])) {
        $data = array(
            'names' => $_POST['yourname'],
            'emails'    => $_POST['customer_email'],
            'gender' => $_POST['customer_gender'],
            'age' => $_POST['customer_age']
        );

        $success=$wpdb->insert( $table, $data );
        if($success){
            echo 'data has been save' ; 
        }
    else {
            echo "data not save";
        }
        } 
        ?>
        <div class="container">
            <div class="row ">
                <div class="col-md-12 pb-5">
                    <form method="post">
                        Your name:<input type="text" name="yourname" id="yourname">
                        Your Email:<input type="text" id="customer_email" name="customer_email"/><br>
                            Gender:<input type="radio" name="customer_gender" value="male">Male
                                   <input type="radio" name="customer_gender" value="female">Female<br>
                            Your Age:<input type="text" name="customer_age" id="customer_age"><br>
                            <input type="submit" name="submit" value="Submit">
                           <input type="submit" name="show" value="Show"> <a href="http://localhost/vishal_nov/edit-form/">show</a>
                    </form>
                </div>
            </div>
        </div>
        
<?php
    get_footer();   
?>

this is update and delete query

<?php
 /* 
  * Template Name: Show Form Template Page
  */
?>
 <div class="container">
    <div class="row">
        <div class="col-md-12">
            <form method="POST">
            <table border="1">
                <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Email</th>
                <th>Gender</th>
                <th>Age</th>
                <th>Edit</th>
                <th>Delete</th>
                </tr> 
                <?php
                global $wpdb;
                $table= 'wp_contact_form';
                $result = $wpdb->get_results ( "SELECT * FROM $table" );

                if(!empty($result)){
                foreach ( $result as $print ) {                                         
        ?>
                <tr>
                <td><?php echo $print->id;?></td>
                <td><?php echo $print->names;?></td>
                <td><?php echo $print->emails;?></td>
                <td><?php echo $print->gender;?></td>
                <td><?php echo $print->age;?></td>
                <td><button class="" name="update">Edit</button></td>
                <td><a href="#" id="'.$print->id.'" class="del_btn">Delete</a></td>
                </tr>
                <?php
                }
                } 
                ?>
        </table>
        </form>
                <?php
                global $wpdb;
                $id= $print->id;
                $table= 'wp_contact_form';
                if (isset($_POST['update'])) {
                    $data =array(
                            'names' => $_POST['yourname'],
                            'emails'    => $_POST['customer_email'],
                            'gender' => $_POST['customer_gender'],
                            'age' => $_POST['customer_age']
                                );
                    $wherecondition=array(
                                    'id'=>$id
                                );
                $updated=$wpdb->update($table, $data, $wherecondition);

            }   
                
                
                    ?>
        </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>

$(document).ready(function(){
                 
                 $('.del_btn').click(function(){
                   var el = this;
                               
                   var deleteid = $(this).attr('id');
                 
                   var confirmalert = confirm("Are you sure?");
                   if (confirmalert == true) {
                      // AJAX Request
                      $.ajax({
                        url: 'delete1.php',
                        type: 'POST',
                        data: { dell_id:deleteid },
                        success: function(response){

                          if(response == 1){
                        
                        $(el).closest('tr').css('background','tomato');
                        $(el).closest('tr').fadeOut(800,function(){
                           $(this).remove();
                        });
                          }
                          else{
                        alert('Invalid ID.');
                          }

                        }
                      });
                  }
                   });

                 });
</script>
    </div>
</div>

delete query define in delete.php file but it shows page not found

    <?php
global $wpdb;
$table= 'wp_contact_form';
$id=$_POST['dell_id'];
$result = $wpdb->delete($table, array('id' => $id));

if(!empty($result))
 {
     if($result){
    echo "success";
     }
   
     
}

?>

here is screen shot https://prnt.sc/wbgzgt
here is screenshot for error page not found for ajax jquery in console
https://prnt.sc/wc1l4s

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

On the line where you’re defining $id:

<?php
$id= $result->id;
global $wpdb;

You’re getting the $id field in $result. $result is an array of the objects returned from the database get_results call. It looks like you want to use the $print variable for the current row in the loop, so $id = $print->id.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x