0

私がやろうとしているのは、テキスト ファイルの各行を mysql データベースの新しい行に挿入することです。私は何を間違っていますか?

次のようなテキスト ファイルがあります。

11111,customer1
11112,customer2
11113,customer3
11114,customer4

私のMySQL DBには、フィールド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>
4

5 に答える 5

4

あなたの値は、では$arrMなく配列にあり$_POSTます。これを試してください:

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

$sqlまた、これをループで実行することを確認する必要があります。

于 2013-08-05T03:29:10.057 に答える
0

スクリプトには複数の問題があります。

  1. まず、使用していますが、送信ボタンで$_POST['submit']使用していません。name='submit'
  2. @vinod が言ったように、値は配列$arrM not in$_POST` にあります。

次に、データをループに挿入する必要があります。接続を 1 回だけ含めたことを確認し、すべてのデータベース操作が完了したら接続を閉じます。

<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>
于 2013-08-05T04:11:12.687 に答える
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();
  }
于 2013-08-05T04:22:30.773 に答える
0

値を正しく挿入する必要があります

$sql="INSERT INTO list (number, customer) VALUES ('".$arrM[0]."','".$arrM[1]."')"; 
于 2018-06-06T04:55:19.547 に答える