Update database record in plugin

admin2025-01-08  4

I am developing a plugin now i want to update my table record.... I'm using this code to fetch data from database

But i need to update data... also i am sending the screenshot of the database.... please help me

and here I'm trying to update it

<?php
$id = $_GET['id'];
$dirname = dirname(__FILE__);
$root = false !== mb_strpos( $dirname, 'wp-content' ) ? mb_substr( $dirname, 0, mb_strpos( $dirname, 'wp-content' ) ) : $dirname;
require_once( $root . "wp-load.php" );


    global $wpdb;
     $post_name_table = $wpdb->prefix . "tropix_dmin";
     $query = "
        SELECT post_name1, id
        FROM $post_name_table
        WHERE id= $id";
    $post_name1_results = $wpdb->get_results($query); 



    $arr = serialize($_POST);

    global $wpdb;
        $data = array( 
            'post_name1'    => $arr,
            );

        $wpdb->update(
            $wpdb->prefix . 'tropix_dmin',
            $data
        );


?>

    <table class="widefat" style="border-radius:4px;">

  <tr>
    <th class="manage-column" scope="col" style="font-weight:bold; font-size:20px;">Field</th>
    <th class="manage-column" scope="col" style="font-weight:bold; font-size:20px;">Value</th>
  </tr>
  <?php foreach ( $post_name1_results as $post_names ) {

    $this_data = unserialize($post_names->post_name1);
    //echo "<pre>";
    //print_r($this_data);

    if($this_data != ""){

   ?>    <form action="" method="POST">
    <div class="form">
    <input type="submit" value="SUBMIT">
  </div>
  <!--col2 ends here-->
  </section>

  <tr class="alternate iedit">
    <td class="column-columnname">Street Address</td>
    <td class="column-columnname"><input type="text" value="<?php echo $this_data['Street_Address']; ?>" /></td>
  </tr>
 <?php }
  } ?>
    </table>

I am developing a plugin now i want to update my table record.... I'm using this code to fetch data from database

But i need to update data... also i am sending the screenshot of the database.... please help me

and here I'm trying to update it

<?php
$id = $_GET['id'];
$dirname = dirname(__FILE__);
$root = false !== mb_strpos( $dirname, 'wp-content' ) ? mb_substr( $dirname, 0, mb_strpos( $dirname, 'wp-content' ) ) : $dirname;
require_once( $root . "wp-load.php" );


    global $wpdb;
     $post_name_table = $wpdb->prefix . "tropix_dmin";
     $query = "
        SELECT post_name1, id
        FROM $post_name_table
        WHERE id= $id";
    $post_name1_results = $wpdb->get_results($query); 



    $arr = serialize($_POST);

    global $wpdb;
        $data = array( 
            'post_name1'    => $arr,
            );

        $wpdb->update(
            $wpdb->prefix . 'tropix_dmin',
            $data
        );


?>

    <table class="widefat" style="border-radius:4px;">

  <tr>
    <th class="manage-column" scope="col" style="font-weight:bold; font-size:20px;">Field</th>
    <th class="manage-column" scope="col" style="font-weight:bold; font-size:20px;">Value</th>
  </tr>
  <?php foreach ( $post_name1_results as $post_names ) {

    $this_data = unserialize($post_names->post_name1);
    //echo "<pre>";
    //print_r($this_data);

    if($this_data != ""){

   ?>    <form action="" method="POST">
    <div class="form">
    <input type="submit" value="SUBMIT">
  </div>
  <!--col2 ends here-->
  </section>

  <tr class="alternate iedit">
    <td class="column-columnname">Street Address</td>
    <td class="column-columnname"><input type="text" value="<?php echo $this_data['Street_Address']; ?>" /></td>
  </tr>
 <?php }
  } ?>
    </table>
Share Improve this question edited Nov 16, 2017 at 13:09 ali arslan asked Nov 16, 2017 at 11:41 ali arslanali arslan 114 bronze badges 2
  • but where are you trying to update it? I can't see any code to submit the data – Piyush Rawat Commented Nov 16, 2017 at 12:09
  • Thank you, Piyush for the reply..... please check the code again after the image – ali arslan Commented Nov 16, 2017 at 12:23
Add a comment  | 

1 Answer 1

Reset to default 0

It's really hard to understand what are you actually doing ( a refactor will help also).

but for updating you can try: You will need the $id, show you tell which row to update.

$wpdb->update(
    $wpdb->prefix . 'tropix_dmin',
    array( 'post_name1' => $arr),
    array( 'id' => $id ),
    array( '%s' ),
    array( '%d' )
);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266120a1095.html

最新回复(0)