Click here to Skip to main content
15,883,831 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here I want to update a column memberStatus on my users table based on the selected ID. In my foreach statement I have a button activate that I use for my update. The problem is it does not update on the correct row. It takes the first ID not the selected ID. How can I make it UPDATE the right record or row?

What I have tried:

function check_view() {
 global $wpdb;


 $output .= '<table>
  <tr style="color: #fff; background: #08a873">
    <th>   Account ID</th>
    <th>   Name</th>
    <th>Phone</th>
    <th>Email</th>
    <th>Referer ID</th>
    <th> Activate Member</th>
  </tr>';
 $results = $wpdb->get_results("select * from $wpdb->users WHERE memberStatus = 'Not Active'"); 
 $current_date = date('Y-m-d H:i:s');

 foreach( $results as $user_data) {


        $startTimeStamp = strtotime($user_data->deposit_date);
        $endTimeStamp = strtotime($current_date);

        $timeDiff = abs($endTimeStamp - $startTimeStamp);

        $numberDays = $timeDiff/86400;  // 86400 seconds in one day

        // and you might want to convert to integer
        $numberDays = intval($numberDays);

       $roi = $user_data->amount * 2;  
       $amount_growth = $roi - $user_data->amount;

 $output .= "<tr>
    <td> $user_data->ID</td>
    <td> $user_data->user_nicename</td>
    <td> $user_data->Phone</td>
    <td> $user_data->user_email</td>
    <td> $user_data->refID</td>
    <td> <form method='post'> <input type='submit' name='submit' value='Activate' /> </form> </td>
  </tr>";


 if($_POST['submit']) {

    $todate = date('Y-m-d H:i:s');   

    //Update Member Status
    $status = "Active";
    $where = array('ID' => $user_data->ID);
        $subs = array('user_registered' =>  $todate, 'memberStatus'=> $status);
        $table_name  = $wpdb->prefix."users";

       $actMember = $wpdb->update($table_name, $subs, $where);


     if($actMember) {
       header("Refresh:0");
       exit;
          } else {
       echo 'not';
       }
 }

 }
 $output .= '</table>';

 return $output;
}
Posted
Updated 12-Nov-19 6:22am
v2
Comments
Afzaal Ahmad Zeeshan 9-Nov-19 15:35pm    
I don't see where you are submitting the user id in the request.
Member 14649933 9-Nov-19 20:48pm    
The user id I am using in the WHERE clause is the same user ID that I am retrieving in my foreach loop. I am not sure if that was your concern.

1 solution

As Afzaal said, you're not sending the user ID when you click on the button. You need to add a hidden input to the form with the user ID you want to process:
PHP
$output .= "<tr>
    <td> $user_data->ID</td>
    <td> $user_data->user_nicename</td>
    <td> $user_data->Phone</td>
    <td> $user_data->user_email</td>
    <td> $user_data->refID</td>
    <td> <form method='post'> <input type='hidden' name='ID' value='$user_data->ID' /> <input type='submit' name='submit' value='Activate' /> </form> </td>
  </tr>";
You'll also need to move your if($_POST['submit']) block outside of the foreach loop - preferably before the $wpdb->get_results call, so that the data you display reflects the update you've just made.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900