I am using a MySQL query from inside a WordPress custom plugin. I am submitting two values when posting. I want to update the time column if the user exists in the database.
Here is my code
function main()
{
if (isset($_REQUEST)) {
global $wpdb;
$taskTime = $_REQUEST["task_current_time"];
$taskTitle = $_REQUEST["task_current_title"];
$select_all_current = $wpdb->prepare("SELECT * FROM $this->tablenamev2 WHERE task_current_title = '$taskTitle'");
if ($wpdb->get_results($select_all_current)) {
var_dump("USER EXIST");
$wpdb->prepare("UPDATE $this->tablenamev2 SET task_current_time = '$taskTime' WHERE task_current_title = '$taskTitle'");
return;
} else {
var_dump("USER DOES NOT EXIT!!!");
$wpdb->query("INSERT INTO $this->tablenamev2 (task_current_time, task_current_title) VALUES ('$taskTime', '$taskTitle')");
die();
}
}
}
CodePudding user response:
The mistake was in $select_all_current = $wpdb->prepare() and instead I replaced it with $wpdb->query();
Thank you for ur help!
CodePudding user response:
I have modified your code for update and insert(hope it helps)....
function main()
{
if (isset($_REQUEST)) {
global $wpdb;
$table_px = $this->tablenamev2;
// Hope tablenamev2 is the table name.....
$taskTime = $_REQUEST["task_current_time"];
$taskTitle = $_REQUEST["task_current_title"];
$select_all_current = $wpdb->prepare("SELECT * FROM $this->tablenamev2 WHERE task_current_title = '$taskTitle'");
if ($wpdb->get_results($select_all_current)) {
var_dump("USER EXIST");
$wpdb->query( $wpdb->prepare( "
UPDATE `%s` SET `task_current_time` = %s WHERE `task_current_title` = %s )",
$table_px, $taskTime, $taskTitle
) );
return;
} else {
var_dump("USER DOES NOT EXIT!!!");
$data = array('task_current_time' => $taskTime, 'task_current_title' => $taskTitle);
$format = array('%s','%s');
$wpdb->insert($table_px,$data,$format);
die();
}
Let me know if it working..