I have a file that contains data from a device, and I need to insert this data into a database. The text file looks like this:
10/01/15-20:37:30 = 40.2,25.4,42.3,54.3,70.0,66.6
10/01/15-20:38:26 = 67.3,18.4,30.6,39.3,70.0,66.6
10/01/15-20:39:21 = 74.5,16.8,28.2,36.0,70.0,66.6
10/01/15-20:40:16 = 76.8,16.6,27.6,35.2,70.0,66.6
10/01/15-20:41:12 = 78.1,16.4,27.3,34.9,70.0,66.6
And my code currently looks like this:
<?php
$servername = "localhost";
$username = "root";
$password = "pass";
$dbname = "dbase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<?PHP
$file_handle = fopen("datalog.txt", "r");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(' ', $line_of_text);
print $parts[0] . $parts[1]. "<BR>";
}
$datainsert = "INSERT INTO reading (val1, val2, val3, val4, val5, val6, val7, val8, val9, val10) VALUES ($parts[0])";
if (mysqli_query($conn, $datainsert)) {
echo "New record created successfully";
} else {
echo "Error: " . $datainsert . "<br>" . mysqli_error($conn);
}
?>
<?php
$conn->close();
?>
The error I get is this:
Error:
INSERT INTO reading (Box_ID, Time_Stamp, Temperature, Humidity, CO_Level, VOC_Reading_UOM, VOC_Reading_V1, VOC_Reading_V2, VOC_Reading_V3, VOC_Reading_V4) VALUES () Column count doesn't match value count at row 1
My database and site do connect without errors, and this page is the only one with problems. What is going wrong?