Change select query to update

  mysql, php

I’m struggling with changing this to an update query i’ve tried to delete the query and build a new one and then later on call ex. $json[$i][‘timestamp’] ?? 0); without luck I want it to do like -> "UPDATE sqltable SET stuff here WHERE ident=’$json[$i][‘ident’] ?? 0)’;

$mysql_table='cars';

$itms = [];
    // Prepare messages
    for ($i=0; $i < count($json); $i++) {
      $itms[] = '(
        "' . mysqli_real_escape_string($con, $json[$i]['ident'] ?? '') . '",
        ' . floatval($json[$i]['server.timestamp'] ?? 0) . ',
        ' . floatval($json[$i]['position.longitude'] ?? 0) . ',
        ' . floatval($json[$i]['position.latitude'] ?? 0) . ',
        ' . floatval($json[$i]['timestamp'] ?? 0) . ',
        ' . floatval($json[$i]['position.altitude'] ?? 0) . ',
        ' . floatval($json[$i]['position.direction'] ?? 0) . ',
        ' . floatval($json[$i]['position.speed'] ?? 0) . ',
        ' . floatval($json[$i]['fuel.level'] ?? 0) . ',
        ' . floatval($json[$i]['iot.battery.level'] ?? 0) . ',
        ' . floatval($json[$i]['actual.remaining.mileage'] ?? 0) . ',
        ' . intval($json[$i]['position.satellites'] ?? 0) . '
      )';
    }

    $query = '
    INSERT INTO ' . $mysql_table . ' (
      `ident`,
      `server.timestamp`,
      `position.longitude`,
      `position.latitude`,
      `timestamp`,
      `position.altitude`,
      `position.direction`,
      `position.speed`,
      `fuel.level`,
      `iot.battery.level`,
      `actual.remaining.mileage`,
      `position.satellites`
    ) VALUES ' . join(',', $itms);

Source: Ask PHP

LEAVE A COMMENT