Insert or update

Posted under » PHP on 20 November 2022

Lets continue from processing Apache log files.

I've put the date as index, so it will not overwrite if the record is existing. As usual, here is the insert command, where fossa is the table created for the Apache log file.

mysqli_query($dbhandle, "INSERT INTO `fossa` (`bila`, `ip`, `sms`) VALUES ('$date', '$pizza[0]', '$trimmed')");

In this example this is just for one row and the tail output is 10 rows. We have to process all the 10 rows.

The whole shebang

require_once __DIR__ .'/konek.php';

$mendel = implode('', file('tail.log'));

$pieces  = explode(PHP_EOL, $mendel);

foreach ($pieces as $key => $val) {

 $pizza = explode(" ", $val);
 $trimmed = substr($pizza[3], 1);

 $date = str_replace("x",' ',$pizza[1]);

 mysqli_query($dbhandle, "INSERT INTO `fossa` (`bila`, `ip`, `sms`) VALUES ('$date', '$pizza[0]', '$trimmed')");

 echo "<p>".$date." has been inserted.";

You don't have to worry about updating this table because the index is on time and there will never be a situation where 2 things happen at the same time.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data