我想要做的是将文本文件中的每一行插入到 mysql 数据库的新行中。我究竟做错了什么?
我有一个类似于以下内容的文本文件。
11111,customer1
11112,customer2
11113,customer3
11114,customer4
我的 MySQL 数据库有字段 id、number、customer
我的 php 代码不起作用,如下所示。
<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>