我正在使用WordPress自定义插件内部的MySQL查询。我提交两个值张贴时。如果用户在数据库中存在,我想更新时间列。
我的代码
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();
}
}
}
错误是在$select_all_current = $wpdb->prepare()中,我将其替换为$wpdb->query();
谢谢你的帮助!
我已经修改了您的代码更新和插入(希望它有帮助)....
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();
}
让我知道它是否有效。