简体   繁体   English

将变量插入MySQL DB表

[英]Insert variable into MySQL DB table

I'm following this tutorial: https://itsolutionstuff.com/post/php-import-excel-file-into-mysql-database-tutorialexample.html 我正在学习本教程: https : //itsolutionstuff.com/post/php-import-excel-file-into-mysql-database-tutorialexample.html

It uses this library: https://github.com/nuovo/spreadsheet-reader 它使用以下库: https : //github.com/nuovo/spreadsheet-reader

Most of the code seems to work. 大多数代码似乎都能正常工作。 I've created a page where you can select a file from your computer and "upload" it. 我创建了一个页面,您可以在其中选择计算机中的文件并“上传”它。 We then create a table of the data in the file on our page. 然后,我们在页面上的文件中创建一个数据表。

The problem is at the last and most important function: importing the data to the local MySQL database. 问题出在最后也是最重要的功能上:将数据导入本地MySQL数据库。

My connection looks like this: 我的连接如下所示:

<?php
    $servername = "localhost";
    $dbname = "analyse";
    $password = "";
    $username = "root";
    $mysqli = new mysqli("localhost", "root", "", "analyse");
?>

I'm using the root account which have no password. 我正在使用没有密码的root帐户。 The database is called "analyse". 该数据库称为“分析”。 The table we are going to import data to is called "test". 我们将要导入数据的表称为“测试”。 I've tried with another account I created which had the username "import" and password "123" and it didn't give any results either. 我尝试使用创建的另一个帐户使用用户名“ import”和密码“ 123”,但也没有任何结果。

The rest of my PHP script looks like this: 我的PHP脚本的其余部分如下所示:

<?php
require('library/php-excel-reader/excel_reader2.php');
require('library/SpreadsheetReader.php');
require('db_config.php');

if(isset($_POST['Submit'])){
  $ok = 1;
  $mimes = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet'];
  if($ok = 1); {
    $uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
    move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
    $Reader = new SpreadsheetReader($uploadFilePath);
    $totalSheet = count($Reader->sheets());
    echo "Du har ".$totalSheet." regneark".

    $html="<table border='1'>";
    $html.="<tr>
    <th>Account Number</th>
    <th>forstelisten</th>
    <th>andrelisten</th>
    <th>Account</th>
    </tr>";

    /* For Loop for all sheets */
    for($i=0;$i<$totalSheet;$i++){
      $Reader->ChangeSheet($i);
      foreach ($Reader as $Row)
      {
        $html.="<tr>";
        $accountnumber = isset($Row[0]) ? $Row[0] : '';
        $forstelisten = isset($Row[1]) ? $Row[1] : '';
        $andrelisten = isset($Row[2]) ? $Row[2] : '';
        $account = isset($Row[3]) ? $Row[3] : '';
        $html.="<td>".$accountnumber."</td>";
        $html.="<td>".$forstelisten."</td>";
        $html.="<td>".$andrelisten."</td>";
        $html.="<td>".$account."</td>";
        $html.="</tr>";

        $query = "
        INSERT INTO test(Account Number, forstelisten, andrelisten, Account) 
        VALUES('".$accountnumber."','".$forstelisten."','".$andrelisten."','".$account."')";
        $mysqli->query($query);
       }
    }
    $html.="</table>";
    echo $html;
    echo "<br />Data lagt inn i databasen.";
  }
}
?>

After running this I get the table and the sheet count and the message at the end but nothing shows up in MySQL database and there are no error messages. 运行此命令后,我得到了表,工作表计数和最后的消息,但MySQL数据库中没有任何显示,也没有错误消息。

The table we are trying to import to (test) have four colons: Account Number, forstelisten, andrelisten, Account. 我们尝试导入到(测试)的表具有四个冒号:帐号,forstelisten和rerelisten,Account。 They are all int. 他们都是int。 The xlsx file we are trying to import is called test. 我们尝试导入的xlsx文件称为test。 It contains four colons and three rows. 它包含四个冒号和三行。 All these cells only have numbers in them (different numbers from 1 to 300, no decimals). 所有这些单元格中都只有数字(数字从1到300,无小数)。

The MySQL database is running on WAMP server. MySQL数据库正在WAMP服务器上运行。 PHP version is 7.1.9. PHP版本是7.1.9。 The MySQL version is 5.7.19. MySQL版本是5.7.19。

Please ask if you need any more information and thank you for your help. 请询问您是否需要更多信息,并感谢您的帮助。

EDIT: 编辑:

It now works! 现在可以使用了! Thank you everyone. 谢谢大家。

The working code looks like this: 工作代码如下所示:

<?php
require('library/php-excel-reader/excel_reader2.php');
require('library/SpreadsheetReader.php');
require('db_config.php');

if(isset($_POST['Submit'])){
  $ok = 1;
  $mimes = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet'];
  if($ok == 1); {
    $uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
    move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
    $Reader = new SpreadsheetReader($uploadFilePath);
    $totalSheet = count($Reader->sheets());
    echo "Du har ".$totalSheet." regneark".

    $html="<table border='1'>";
    $html.="<tr>
    <th>Account Number</th>
    <th>forstelisten</th>
    <th>andrelisten</th>
    <th>Account</th>
    </tr>";

    /* For Loop for all sheets */
    for($i=0;$i<$totalSheet;$i++){
      $Reader->ChangeSheet($i);
      foreach ($Reader as $Row)
      {
        $html.="<tr>";
        $accountnumber = isset($Row[0]) ? $Row[0] : '';
        $forstelisten = isset($Row[1]) ? $Row[1] : '';
        $andrelisten = isset($Row[2]) ? $Row[2] : '';
        $account = isset($Row[3]) ? $Row[3] : '';
        $html.="<td>".$accountnumber."</td>";
        $html.="<td>".$forstelisten."</td>";
        $html.="<td>".$andrelisten."</td>";
        $html.="<td>".$account."</td>";
        $html.="</tr>";

        $query = "
    INSERT INTO `analyse`.`test`(`Account Number`, `forstelisten`, `andrelisten`, `Account`) 
    VALUES('$accountnumber','$forstelisten','$andrelisten','$account')";
        $mysqli->query($query);
       }
    }
    $html.="</table>";
    echo $html;
    echo "<br />Data lagt inn i databasen.";
  }//else { 
    //die("<br/>Sorry, File type is not allowed. Only Excel file."); 
  //}
}
?>

我认为您的列名称(帐户编号)是两个字,因此请尝试对不符合标准的字段名称使用反引号,如下所示,并使用($ ok == 1)更改条件($ ok = 1)

$query = "INSERT INTO test(`Account Number`, forstelisten, andrelisten, Account) VALUES ('".$accountnumber."','".$forstelisten."','".$andrelisten."','".$account."')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM