adnan
adnan

Reputation: 1385

wpdb update query not working

I've made an email script that should update as soon as wp_mail has result. For some reason my value won't update. Have I missed something? I am receiving the mail so the wp_mail works.

Cheers!

$email_result = wp_mail( $to, $subject, $message, $headers );

if( $email_result ){//wp_mail() processed the request successfully
    global $wpdb;
    $table_name = $wpdb->prefix . "wpsc_coupon_codes";
    $coupon_id = $ereminder->ID;

$ereminders = $wpdb->query( $wpdb->prepare("
    UPDATE *
    FROM $table_name
    SET reminder = 1
    WHERE ID = $coupon_id
") );

}

Upvotes: 6

Views: 70685

Answers (7)

MD TAREK HOSSEN
MD TAREK HOSSEN

Reputation: 419

Wordpress provides a function to update data of tables. You can also check here

wpdb::update( string $table, array $data, array $where, string[]|string $format = null, string[]|string $where_format = null ): int|false

Example:

global $wpdb;
$wpdb->update($table,$data,$where);

$table : The table name.

$data : A PHP array where the keys are the columns and the values are the the values to be inserted into those columns.

$where : A PHP array where the key is the column name, and the value is the value to be checked.

Upvotes: 0

CristianR
CristianR

Reputation: 489

We could use $wpdb->update, like this:

 global $wpdb;
 $table_name = $wpdb->prefix.'your_table_name';
 $data_update = array('row_name_1' => $row_data_1 ,'row_name_2' => $row_data_2);
 $data_where = array('row_name' => $row_data);
 $wpdb->update($table_name , $data_update, $data_where);

Upvotes: 5

ABHISHEK GUPTA
ABHISHEK GUPTA

Reputation: 71

<?php 
  global $wpdb;
    if(isset($_POST['progress'])){
    $table=t_test;
    $data=array('client_development'=>$_POST['Progress']);
    $where=array('p_id'=>$_SESSION['id']);
    $format=("%d");
    $whereFormat=("%d");
    $result4=$wpdb->UPDATE($table,$data,$where,$format,$whereFormat);
  }
?>
<form method="post">
  <input type="text" name="Progress">
  <input type="submit" name="progress" value="Prog%">
</form>
<?php 
if(isset($_POST['progress'])){
$table=t_test;
    $data=array('client_development'=>$_POST['Progress']);
    $where=array('p_id'=>$_SESSION['id']);
    $format=("%d");
    $whereFormat=("%d");
    $result4=$wpdb->UPDATE($table,$data,$where,$format,$whereFormat);
}
?>
<form method="post">
  <input type="text" name="Progress">
  <input type="submit" name="progress" value="Prog%">
</form>

Upvotes: 0

Goran Jakovljevic
Goran Jakovljevic

Reputation: 2820

Example of mine that is working:

$result = $wpdb->update(
    $wpdb->prefix .'sae_calendar', 
    array( 
        'year' => $year,
        'quarter' => $quarter,
        'start_date' => $start_date,
        'end_date' => $end_date,
        'reservation_start_date' => $reservation_start_date,
        'reservation_end_date' => $reservation_end_date 
    ), 
    array(
        "id" => $id
    ) 
);

Upvotes: 3

Try This:

$wpdb->update( $table_name, array( 'reminder' => 1),array('ID'=>$coupon_id));

Upvotes: 27

RajSar
RajSar

Reputation: 77

You may change instead of (UPDATE * FROM)

$ereminders = $wpdb->query($wpdb->prepare("UPDATE $table_name SET reminer='1' WHERE ID=$coupon_id"));

and use no break.

Thank you.

Upvotes: 4

echo_Me
echo_Me

Reputation: 37233

try this

UPDATE  $table_name
SET reminer = 1
WHERE ID = $coupon_id

Upvotes: 6

Related Questions