0

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 enter image description here

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>
  • but where are you trying to update it? I can't see any code to submit the data – Piyush Rawat Nov 16 '17 at 12:09
  • Thank you, Piyush for the reply..... please check the code again after the image – ali arslan Nov 16 '17 at 12:23
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' )
);
  • Drupalizeme thankx for the answer i am just fetching data from database and showing it into the input box and then trying to update it..... with your code when i update it just sending an empty field data – ali arslan Nov 16 '17 at 13:15
  • and all other data is now automatically deleting when i update it with your code – ali arslan Nov 16 '17 at 13:17
  • This is an example you should make it work with your code. For example, updating you should have the required $id and also the valid $arr data to work. If for some reason the $arr is empty it should add an empty value. This mean you should check first if(!empty($arr)) {..} so you have some data before trying to update. – Drupalizeme Nov 16 '17 at 13:20

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.