What I'm trying to do is insert each line from a text file into a new row of a mysql database. What am I doing wrong?
I have a text file that looks something like the following.
11111,customer1
11112,customer2
11113,customer3
11114,customer4
My MySQL DB has the fields id, number, customer
My php code which isn't working looks like the following.
<html>
<head>
<title>Add File To DB</title>
</head>
<body>
<form action="list.php" method="post">
<input type="submit" value="Submit File" />
<table>
<?php
$f = fopen("textfile.txt", "r") or exit("Unable to open file!");
// Read line by line until end of file
while (!feof($f)) {
// Make an array using comma as delimiter
$arrM = explode(',',fgets($f));
// Write links (get the data in the array)
echo '<tr><td name="number">' . $arrM[0] . '</td><td name="customer">' . $arrM[1] . '</td></tr>';
}
fclose($f);
if (isset($_POST['submit'])) {
include 'connection.php';
$sql="INSERT INTO list (number, customer) VALUES ('$_POST[number]','$_POST[customer]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error());
}
mysqli_close($con);
}
?>
</table>
<input type="submit" value="Submit File" />
</form>
</body>
</html>