Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a .txt file split into lines, and the items for each line are split into arrays log_dates and log_times. I'm trying to insert each log date and it's matching log time into my database which has log_date and log_time columns.

Access.txt

2014-03-16 13:57:35.089
2014-03-16 13:57:35.089
2014-03-16 13:57:35.089
2014-03-16 13:57:35.089  

LogsModel.php

function __construct($db) {
    try {
        $this->db = $db;
    } catch (PDOException $e) {
        exit('Database connection could not be established.');
    }
}

public function insertFileContents()
{
    $filename = 'C:/Program Files/FileMaker/FileMaker Server/Logs/Access.log';

    // Open the file
    $fp = @fopen($filename, 'r');

    // Add each line to an array
    if ($fp) {
        $lines = explode("\n", fread($fp, filesize($filename)));

        foreach($lines as $line){
            $l = explode(" ", $line);
            $log_dates[] = $l[0];
            $log_times[] = $l[1];

            $sql = ("INSERT INTO log_table VALUES (log_date, log_time) VALUES ('$log_dates', '$log_times')");

            $query = $this->db->prepare($sql);
            $query->execute();
        }

        return true;
    }

}

Eventually I would like to have it so every time the page is accessed the database gets updated by the Access.txt file which will continue to accumulate new rows.

share|improve this question
1  
You're using PDO, why aren't you using parametrized queries? – Barmar May 23 '14 at 2:10
    
Thanks @Barmar, was just looking for something easy to get the data in. I get a SQL Syntax Error - "Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUES ('2014-03-16', '13:57:35.089')' at line 1 in C:\wamp\www\application\models\logsmodel.php on line 53" – Aaron Turecki May 23 '14 at 2:18
    
VALUES only goes before the list of values, not before the list of column names. – Barmar May 23 '14 at 2:20
    
Comments about my answer should be put below the answer, not the question. – Barmar May 23 '14 at 2:21
    
@Barmar - apologies, that was stupid of me... data is being inserted now, thanks! – Aaron Turecki May 23 '14 at 2:22
up vote 1 down vote accepted

$log_dates and $log_times shouldn't be arrays, they should be string variables:

    foreach($lines as $line){
        $l = explode(" ", $line);
        $log_dates = $l[0];
        $log_times = $l[1];

        $sql = ("INSERT INTO log_table (log_date, log_time) VALUES ('$log_dates', '$log_times')");

        $query = $this->db->prepare($sql);
        $query->execute();
    }

You also had an extra VALUES keyword before the list of column names.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.