Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文本文件php mysql向多行插入数据_Php_Mysql_Web - Fatal编程技术网

从文本文件php mysql向多行插入数据

从文本文件php mysql向多行插入数据,php,mysql,web,Php,Mysql,Web,我要做的是将文本文件中的每一行插入mysql数据库的新行。我做错了什么 我有一个如下所示的文本文件 11111,customer1 11112,customer2 11113,customer3 11114,customer4 <html> <head> <title>Add File To DB</title> </head> <body> <form action="list.php" method="post

我要做的是将文本文件中的每一行插入mysql数据库的新行。我做错了什么

我有一个如下所示的文本文件

11111,customer1
11112,customer2
11113,customer3
11114,customer4
<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>
My MySQL DB包含id、number、customer字段

我的php代码不工作,如下所示

11111,customer1
11112,customer2
11113,customer3
11114,customer4
<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
中,不在
$\u POST
中。请试试这个:

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

您还需要确保在循环中运行此
$sql

您的值在数组
$arrM
中,而不是
$\u POST
中。请试试这个:

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

您还需要确保在循环中运行此
$sql

您的代码中存在多个问题。如果您只想从文本文件插入,可以尝试以下操作

<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!");
    $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>

将文件添加到数据库

您的脚本存在多个问题

  • 首先,您正在使用
    $\u POST['submit']
    ,但尚未在提交按钮中使用
    name='submit'
  • 正如@vinod所说,您的值在数组中
    $arrM不在
    $\u 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>
    
    
    将文件添加到数据库
    
    您的脚本存在多个问题

  • 首先,您正在使用
    $\u POST['submit']
    ,但尚未在提交按钮中使用
    name='submit'
  • 正如@vinod所说,您的值在数组中
    $arrM不在
    $\u 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>
    
    
    将文件添加到数据库
    
    如果要将文本文件的数据插入数据库,则可以执行此操作。
    
    如果要将文本文件的数据插入数据库,则可以执行此操作。
    
    您必须正确插入值

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

    您必须正确插入值

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

    $arrM
    $sql=
    行中不可见
    $arrM
    $sql=
    行中不可见更正刚刚学习。如果我想要一个在底部,一个在顶部,我可以这样做吗?或者to按钮会相互冲突吗?您可以有多个submit。这不会产生冲突,两者都会起作用。正确的做法就是学习。如果我想要一个在底部,一个在顶部,我可以这样做吗?或者to按钮会相互冲突吗?您可以有多个submit。这不会产生冲突,两者都会起作用。