why doesn’t this update part of this plugin work? it take me to nothing here page

Question

Below is my plugin. Everything works except the update part. It will show the little table with the existing values and when I click on the update button in that little table, it takes me to Nothing Here page, instead of calling the page again.

The url doesn’t update to the page url, it still has the parameter on the end of the url too. Thanks in advanced for your time.

Dean-O

<?php
/*
Plugin Name: deano CRUD Operations
Plugin URI: 
Description: A simple plugin that allows you to perform CRUD operations on custom database table
Version: 1.0.0
Author: Dean-O
Author URI: 
License: GPL2
*/




function deanocrud() {
  global $wpdb;
  //$table_name = $wpdb->prefix . 'books';
  $table_name="books";

  // get current user ID, with default value, if empty
  $current_user_id = get_current_user_id();
  error_log('current_user_id '.$current_user_id);

  if (isset($_POST['newsubmit'])) {

    error_log('newsubmit here');
    $wp_id = $_POST['wp_id'];
    $title = $_POST['title'];
    $author = $_POST['author'];
    $wpdb->query("INSERT INTO $table_name(wp_id,title,author) VALUES('$wp_id','$title','$author')");
    //echo "<script>location.replace('admin.php?page=crud.php');</script>";
    //wp_redirect( "http://localhost/tadpolewp/deano-plugin-successful/" );
    //wp_redirect( "http://localhost/tadpolewp/deano-crud/" );
        ?>
    <script type="text/javascript">
      document.location.href="http://localhost/tadpolewp/deano-crud/";
  </script>
  <?php
    
  }
  if (isset($_POST['uptsubmit'])) {
    error_log('uptsubmit here');
    $id = $_POST['uptid'];
    $title = $_POST['title'];
    $author = $_POST['author'];
    error_log('upt_id = '.$id);
    error_log('title=".$title);
    error_log("author=".$author);
    $wpdb->query("UPDATE $table_name SET title="$title',author="$author" WHERE id='$id'");
    //echo "<script>location.replace('admin.php?page=crud.php');</script>";
    //echo "<script>location.replace('http://localhost/tadpolewp/deano-crud/');</script>";
          ?>
    <script type="text/javascript">
      document.location.href="http://localhost/tadpolewp/deano-crud/";
  </script>
  <?php
    
  }
  if (isset($_GET['del'])) {
    $del_id = $_GET['del'];
    $wpdb->query("DELETE FROM $table_name WHERE user_id='$del_id'");
    //echo "<script>location.replace('admin.php?page=crud.php');</script>";
    echo "<script>location.replace('http://localhost/tadpolewp/deano-crud/');</script>";
  }
  ?>
  <div class="wrap">
    <h2>CRUD Operations</h2>
    <table class="wp-list-table widefat striped">
      <thead>
        <tr>
          
          <th width="25%">USER_ID1</th>
          <th width="25%">__</th>
          <th width="25%">Title</th>
          <th width="25%">Author</th>
          <th width="25%">Actions</th>
        </tr>
      </thead>
      <tbody>
        <form action="" method="post">
          <tr>
            <td><input type="text" value="AUTO_GENERATED" disabled></td>
            <td><input type="hidden" name="wp_id" value="<?php echo esc_attr( $current_user_id ); ?>" ></td>
    
            <td><input type="text" id="title" name="title"></td>
            <td><input type="text" id="author" name="author"></td>
            <td><button id="newsubmit" name="newsubmit" type="submit">INSERT</button></td>
          </tr>
        </form>
        <?php
          
          $result = $wpdb->get_results("SELECT * FROM $table_name where wp_id='$current_user_id'");
          foreach ($result as $print) {
            echo "
            
              <tr>
                
                <td width="25%">$print->wp_id</td>
                <td width="25%"></td>
                <td width="25%">$print->title</td>
                <td width="25%">$print->author</td>
                <td width="25%"><a href="https://wordpress.stackexchange.com/questions/403203/index.php?page=deano-crud.php&upt=$print->id"><button type="button">UPDATE</button> <a href="index.php?page=deano-crud.php&del=$print->id"><button type="button">DELETE</button></a></td>
              </tr>
              
           ";
          }
        ?>

      </tbody>  
    </table>
    <br>
    <br>
    <?php
      if (isset($_GET['upt'])) {
        $upt_id = $_GET['upt'];
        error_log('upt_id = '.$upt_id);
        $result = $wpdb->get_results("SELECT * FROM $table_name WHERE id='$upt_id'");
        foreach($result as $print) {
          $title = $print->title;
          $author = $print->author;
           error_log('title=".$title);    
           error_log("author=".$author);
           error_log("id = '.$print->id);
        }
        echo "
        <table class="wp-list-table widefat striped">
          <thead>
            <tr>
              <th width="25%">ID</th>
              <th width="25%">Title</th>
              <th width="25%">Author</th>
              <th width="25%">Actions</th>
            </tr>
          </thead>
          <tbody>
            <form action='' method='post'>
              <tr>
                <td width="25%">$print->id <input type="hidden" id='uptid' name="uptid" value="$print->id"></td>
                <td width="25%"><input type="text" id='title' name="title" value="$print->title"></td>
                <td width="25%"><input type="text" id='author' name="author" value="$print->author"></td>
                <td width="25%"><button id='uptsubmit' name="uptsubmit" type="submit">UPDATE</button> <a href="index.php?page=deano-crud"><button type="button">CANCEL</button></a></td>
              </tr>
            </form>
          </tbody>
        </table>";
      }
    ?>
  </div>
  <?php
}
add_shortcode('deanocrud_sc','deanocrud');

0
Dean-O 6 months 2022-02-27T11:37:50-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse