Php 双重密钥更新时插入tbl-插入空行?

Php 双重密钥更新时插入tbl-插入空行?,php,mysqli,Php,Mysqli,这个MySQLi语句有什么问题 我想在重复密钥更新时插入 模式 'UID', 'char(17)', 'NO', 'PRI', NULL, '' 'stationID', 'int(11)', 'YES', '', NULL, '' 'temperature', 'float', 'YES', '', NULL, '' 'UV', 'float', 'YES', '', NULL, '' 'temperature_feels', 'float', 'YES', '', NULL, '' 'hum

这个MySQLi语句有什么问题

我想在重复密钥更新时插入

模式

'UID', 'char(17)', 'NO', 'PRI', NULL, ''
'stationID', 'int(11)', 'YES', '', NULL, ''
'temperature', 'float', 'YES', '', NULL, ''
'UV', 'float', 'YES', '', NULL, ''
'temperature_feels', 'float', 'YES', '', NULL, ''
'humidity', 'float', 'YES', '', NULL, ''
'weather_type', 'int(11)', 'YES', '', '-1', ''
'precipitation', 'float', 'YES', '', NULL, ''
'update_station_id', 'tinyint(4)', 'YES', '', '1', ''
'update_due', 'timestamp', 'YES', '', NULL, ''
'weather_status', 'varchar(128)', 'YES', '', NULL, ''
//代码

    $sql = "INSERT INTO weather_data (uv, weather_status, weather_type, temperature, temperature_feels, humidity, precipitation, UID)
            VALUES (uv, weather_status, weather_type, temperature, temperature_feels, humidity, precipitation, UID)
            ON DUPLICATE KEY UPDATE uv = ?, weather_status = ?, weather_type = ?, temperature = ?, temperature_feels = ?, humidity = ?, precipitation = ?, UID = ?";
    $stmt = $dbh->prepare($sql);

    if (!$stmt) {
        throw new \Exception($dbh->error);
    }

    $stmt->bind_param('ssssssss', $uv, $weather_status, $weather_type, $temperature, $temperature_feels, $humidity, $precipitation, $UID);
    $stmt->execute();
    $stmt->close();
似乎每次都插入一个空行?

将查询更新为:

ON DUPLICATE KEY UPDATE 
  uv = VALUES(?),
  weather_status = VALUES(?),
  weather_type = VALUES(?),
  temperature = VALUES(?),
  temperature_feels = VALUES(?),
  humidity = VALUES(?),
  precipitation = VALUES(?),
  UID = VALUES(?)
还有一个问题是,每个
都使用param go,因为在插入之后,您继续使用
,您还需要添加这些参数。使用命名参数PDO可以避免这种情况

$stmt->bind_param('dsidddddidsidddddi', /* first set dsidddddi */ 
  $uv, 
  $weather_status,
  $weather_type,
  $temperature,
  $temperature_feels,
  $humidity,
  $precipitation,
  $UID,
  $uv, // because ? counts further.
  $weather_status,
  $weather_type,
  $temperature,
  $temperature_feels,
  $humidity,
  $precipitation,
  $UID,
);

$stmt->execute();

看起来有点疯狂,但所有字段的初始值都是字符串(排序),而不是占位符。你需要为每一个人绑定

$sql = "INSERT INTO weather_data (uv, weather_status, weather_type, temperature, temperature_feels, humidity, precipitation, UID)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        ON DUPLICATE KEY UPDATE uv = ?, weather_status = ?, weather_type = ?, temperature = ?, temperature_feels = ?, humidity = ?, precipitation = ?, UID = ?";

    $stmt->bind_param('ssssssssssssssss', $uv, $weather_status, $weather_type, $temperature,
                           $temperature_feels, $humidity, $precipitation, $UID, 
                           $uv, $weather_status, $weather_type, $temperature, 
                           $temperature_feels, $humidity, $precipitation, $UID);

在写入插入之前,设置一个select。如果计数为零,则插入else doupdate@Abdulla-这与应该做的恰恰相反,OP正在努力做好这件事。请不要建议不正确的解决方案。我需要通过在重复密钥更新时插入来执行此操作-尽管这将是我的第一个方法-您在哪里设置/绑定插入值?我只看到正在设置/绑定的更新值。插入值的参数在哪里?您只是在那里重复了列名。您应该能够在
UPDATE
partI中使用
uv=VALUES(uv)、weather\u status=VALUES(weather\u status)
等,而不是绑定所有内容两次,尽管如此+1似乎是可行的