0

I am trying to come up with a solution that I believe should be rather simple but I haven't been able to find anything online for this problem specifically.

Basically, I query my database, take that result, explode it, then I need to insert each of the pieces of the array into a separate table. I'm trying to parse data from one long string into multiple parts so that I can further manipulate the data. In this example, the string I have can be separate into 18 different parts. I need to put this into a table with 18 columns. I'm not sure why this isn't inserting.

$sql = "SELECT id, record FROM records WHERE record LIKE 'AA55%' ORDER BY id desc limit 1";
 $result = $conn->query($sql);

 if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
       echo "<div class='container'>".$row['record']. "</div>";
       $pieces = explode(",", $row['record']);
       $headflag = $pieces[0];
       $gprs = $pieces[1];
       $device_id = $pieces[2];
       $event_id=$pieces[3];
       $yymmdd=$pieces[4];
       $hhmmss=$pieces[5];
       $lat = $pieces[6];
       $long = $pieces[7];
       $alt = $pieces[8];
       $spd = $pieces[9];
       $head = $pieces[10];
       $in = $pieces[11];
       $out = $pieces[12];
       $s1 = $pieces[13];
       $vbat = $pieces[14];
       $ad1 = $pieces[15];
       $ad2 = $pieces[16];
       $driver = $pieces[17];


       $insert1 = "INSERT INTO AA55 (HeadFlag, GPRS, unit_id, event_id, yymmdd, hhmmss, latitude, longitude, speed, heading, data_in, data_out, gps_settings, voltage, voltage_2, voltage_3, driver_id)
                  VALUES ('$headflag', '$gprs', '$device_id', '$event_id', '$yymmdd', '$hhmmss', '$lat', '$long', '$alt', '$spd', '$head', '$in', '$out', '$s1', '$vbat', '$ad1', '$ad2', '$driver' )";
       mysql_query($insert1) or trigger_error(mysql_error()." in ".$insert1);
humph2mw
  • 31
  • 1
  • 5

0 Answers0