2013-08-05 84 views
0

我想要做的是將每行從文本文件插入到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> 

回答

4

您的值是在數組$arrM不在$_POST。請試試這個:

$sql="INSERT INTO list (number, customer) VALUES ('$arrM[0]','$arrM[1]')"; 

而且你要確保你在一個循環中運行這個$sql

+0

'$ arrM'不會在 – bansi

1

您的code.If多問題,你想只從文本文件中插入你可以試試下面的

<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!"); 
    $arr_to_insert = array(); 
    // 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>'; 
     //strore text file row to an array 
     $arr_to_insert[] = $arrM; 
    } 

    fclose($f); 
if (isset($_POST['submit'])) { 
include 'connection.php'; 
foreach($arr_to_insert as $ai){ 
    $sql="INSERT INTO list (number, customer) VALUES ('{$ai[0]}','{$ai[1]}')"; 
    if (!mysqli_query($con,$sql)) 
     { 
     die('Error: ' . mysqli_error()); 
     } 

} 
mysqli_close($con); 
} 
?> 
</table> 
</form> 
</body> 
</html> 

代碼的原始版本的問題,評論說。

<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!"); 
    // you need to strore text file row to an array to later insert to database. 
    // Read line by line until end of file 
    while (!feof($f)) { 

    // Make an array using comma as delimiter 
     $arrM = explode(',',fgets($f)); //$arrM is private inside while loop. 
    // 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'; 
//you are trying to insert $_POST[number] and $_POST[customer] which are non-existent 
//also you need to loop through the rows in your text file and store each row. 
$sql="INSERT INTO list (number, customer) VALUES ('$_POST[number]','$_POST[customer]')"; 
if (!mysqli_query($con,$sql)) 
    { 
    die('Error: ' . mysqli_error()); 
    } 

mysqli_close($con); 
} 
?> 
</table> 
<!-- duplicate submit button --> 
<input type="submit" value="Submit File" /> 
</form> 
</body> 
</html> 

PS:請注意我只是指出你的代碼中的錯誤並修復它。我沒有試圖優化它。它看起來像你試圖學習PHP而不是得到更好的解決方案。

+0

的'$ SQL ='線正確的是可見的只是學習。使用如果我想要一個在底部,而在頂部可以這樣做,或者會與按鈕相互衝突? –

+0

您可以有多個提交。它不會發生衝突,兩者都會起作用。 – bansi

0

您的腳本存在多個問題。

  1. 首先,您正在使用$_POST['submit'],但在您的提交按鈕中未使用name='submit'
  2. 由於@vinod表示您的值在數組$arrM not in $ _POST`中。

現在您需要在循環中插入數據。確保只包含一次連接,並在所有數據庫操作完成後關閉連接。

<html> 
<head> 
    <title>Add File To DB</title> 
</head> 

<body> 
    <form action="list.php" method="post"> 
    <input type="submit" name='submit' value="Submit File" /> <!-- Provide name `submit` to your button so that you can access $_POST['submit'] --> 
    <table> 

    <?php 
    $f = fopen("textfile.txt", "r") or exit("Unable to open file!"); 

    //include your connection around here so it is included only once 
    include "connection.php"; 

    // 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>'; 
     if (isset($_POST['submit'])) {    
      $sql = "INSERT INTO list (number, customer) VALUES ('$arrM[0]','$arrM[0]')"; //here should be $arrM 
      if (!mysqli_query($con,$sql)) { 
       die('Error: ' . mysqli_error()); 
      }    
     } 
    } 

    fclose($f); 
    mysqli_close($con); //close your database connection here 
    ?> 
</table> 
<!--<input type="submit" value="Submit File" />--> 
</form> 
</body> 
</html> 
0
If you want to insert data of the text file into the database then you can do this. 
<?php 
include('dbcon.php'); 
$file_content=explode("\n",file_get_contents("demo.txt"));//write your text file name instead of demo.txt 
for($i=0;$i<count($file_content)-1;$i++){ 
    $exploded_content=explode(",",$file_content[$i]); 
    $q=mysqli_query($con,"insert into demo (id,name) values('$exploded_content[0]','$exploded_content[1]')"); 
     //put your table name instead of demo it has two columns id and name 
} 

?> 
Below is the dbcon.php file 
// Create connection 
$con=mysqli_connect("localhost","user","pass","dbname"); 
//put your host user pass database name above i put it dummy 
// Check connection 
if (mysqli_connect_errno($con)) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    }