1

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

4
  • 2
    You're trying to update a post where the id is $result->id, but $result is an array of all your rows. It doesn't have an ID. If you want to update a specific row, you need to send the ID as a form field so that you can tell which row needs to be updated. – Jacob Peattie Dec 18 '20 at 13:19
  • 1
    @JacobPeattie you should post that as an answer rather than a comment – Tom J Nowell Dec 18 '20 at 13:39
  • I understand little bit, but not completely. please explain your code. – vishal Dec 18 '20 at 13:46
  • is update query is wrong? – vishal Dec 23 '20 at 7:47
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.

4
  • Ok, now I understand. but there is still something missing, I replace $print with $result, but nothing works – vishal Dec 19 '20 at 6:15
  • @vishal Did you mean you replaced $result with $print? – Nicholas Harris Dec 20 '20 at 7:35
  • yes, I replaced – vishal Dec 20 '20 at 9:27
  • could you help me @Nicholas Harris – vishal Dec 22 '20 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.