I created this simple function to update a row inside a table called wp_todos
function update_completed_todos($id,$complete) {
global $wpdb;
$table_name = $wpdb->prefix . 'todos';
$result = $wpdb->get_results("SELECT id FROM " . $table_name . " WHERE id = '" .$id."'");
if (count ($result) > 0) {
$wpdb->update(
$table_name,
array(
'complete' => $complete,
),
array( 'id' => $id )
);
}
}
It simply passes 2 variables: $id
and $complete
.
Then I created a foreach
loop:
$todos = $wpdb->get_results("SELECT * FROM " . $table_name);
foreach ($todos as $key => $value) {
<input id="<?php echo $value->id; ?>" type="submit" onclick="<?php update_completed_todos($value->id, 1); ?>" value="<?php esc_attr_e( 'Done' ); ?>" />
}
As you see it should submit "1"
to a specific column complete
in a specific row.
The issue here is when I click on the button it affects all the rows inside the table instead of the target one row. In other word it makes the entire column complete
taking the value of "1".