I just took this code from $wpdb Codex page, and modified my existing Insert query:
$table = $wpdb->prefix . 'user_req';
$post_id = explode('_', $_POST['post_id']);
$user_id = $_POST['user_id'];
$wpdb->insert(
$table,
array(
'user_id' => $user_id,
'post_id' => $post_id[1]
),
array(
'%d',
'%d'
)
);
into:
$wpdb->query( $wpdb->prepare(
"
INSERT INTO {$wpdb->prefix}user_req
( user_id, post_id )
VALUES ( %d, %d )
ON DUPLICATE KEY UPDATE
user_id = VALUES(user_id), post_id = VALUES(post_id)
",
$user_id,
$post_id[1]
) );
Because I need to prevent the duplicate entries entering into the database. But still the duplicate entries are entering into the db while the insert query is functioning properly. To add the ON DUPLICATE KEY UPDATE
portion, I followed the s_ha_dum's Answer on another thread.
What am I doing wrong?
post_id
.