我想要做的是將每行從文本文件插入到mysql數據庫的新行。我究竟做錯了什麼?從文本文件插入數據到多行php mysql
我有一個如下所示的文本文件。
11111,customer1
11112,customer2
11113,customer3
11114,customer4
我的MySQL數據庫有場ID,數量,客戶
我的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>
'$ arrM'不會在 – bansi