我的查询应该在尚未存在的情况下插入一行,但我不知道如何正确放置参数。有8个参数进入新行,但如果进行更新,则只更新6列。 但在进行更新时,它说我提供了错误数量的变量占位符,8个变量的14个占位符。见这里:
$event_id = "value";
$status_type = "value";
$sport_name = "value";
$startdate = "value";
$ut = "value";
$name = "value";
$value_home = "value";
$value_away = "value";
$sql = "INSERT INTO wp_mytable "
. "(event_id,status_type, sport_name, startdate, ut, name, value_home, value_away) "
. "VALUES (%s,%s,%s,%s,%s,%s,%s,%s) "
. "ON DUPLICATE KEY UPDATE status_type = %s, startdate = %s,ut = %s, name = %s, value_home = %s, value_away = %s";
$sql = $wpdb->prepare($sql,$event_id, $status_type,$sport_name,$startdate,$ut ,$name ,$value_home ,$value_away );
$wpdb->query($sql);
我认为插入需要VALUES
个占位符,但它们也会计入重复键的#34;"更新..
我也试着这样写:
"ON DUPLICATE KEY UPDATE status_type = status_type, startdate = startdate,ut = ut, name = name, value_home = value_home, value_away = value_away ";
但是没有用 - 没有更新,但似乎创建并删除了新行。那么正确的查询应该怎么样?
答案 0 :(得分:0)
我找到了一个有效的解决方案:
stackoverflow.com/questions/22699598/prepared-statement-with-on-duplicate-key
更新的每个列都放在“ $sql = "INSERT INTO wp_mytable "
. "(status_type, startdate,ut ,name ,value_home ,value_away ,sport_name ,event_id ) "
. "VALUES (%s,%s,%s,%s,%s,%s,%s,%s) "
. "ON DUPLICATE KEY UPDATE status_type = VALUES(status_type), sport_name=VALUES(sport_name), "
. "startdate = VALUES(startdate),ut = VALUES(ut), name = VALUES(name), "
. "value_home = VALUES(value_home), value_away = VALUES(value_away)";
$sql = $wpdb->prepare($sql,$status_type,$startdate,$ut ,$name ,$value_home ,$value_away, $sport_name, $event_id );
”中。
function sendImage(string $file, int $browser_cache = 2592000) {
if (file_exists($file)) {
header("Content-Type: ". mime_content_type($file));
header("Cache-Control: private, max-age=".$browser_cache);
header('Expires: '.gmdate('D, d M Y H:i:s', time()+$browser_cache).' GMT');
header('Content-Length: '.filesize($file));
readfile($file);
exit;
} else {
echo 'file does not exist';
}
}