【问题标题】:Export HTML table to MySQL [closed]将 HTML 表导出到 MySQL [关闭]
【发布时间】:2013-12-31 08:31:40
【问题描述】:

假设我有一个 HTML 表格。

<html>
<body>
<table border="1">
<tr>
<th>Username</th>
<th>Password</th>
</tr>
<tr>
<td>User_1</td>
<td>Password_1</td>
</tr>
<tr>
<td>User_2</td>
<td>Password_2</td>
</tr>
</table>
</body>
</html>

我需要一个脚本,将 TH 标记中的 COLUMNS 创建到 MySQL 表,然后插入 TD 标记中的数据。该怎么做?

【问题讨论】:

  • &lt;?php # insert code here ?&gt; 是一个好的开始。
  • 是一次性的吗?然后用手去做。只需找到表格编辑器,创建表格并复制和粘贴数据。
  • 它有超过 80k 行。
  • 嘿,请检查我的回答。我测试了它,它可以工作

标签: html mysql sql html-table


【解决方案1】:

这既简单又有趣。

我建议使用 PHP DOM 解析器。 (http://simplehtmldom.sourceforge.net/manual.htm)

  1. 在 MySQL 中手动创建包含其列(id、用户名、密码)的表。
  2. 将 html 文件(带有 html 表格)放在网络服务器上。
  3. 将 PHP DOM 解析器类放在网络服务器上 (http://sourceforge.net/projects/simplehtmldom/files/)
  4. ???
  5. 太棒了!

    require_once ('simple_html_dom.php');
    
    $table = file_get_html('table.html');
    
    foreach($table ->find('tr') as $tr){ // Foreach row in the table!
       $username = $tr->find('td', 0)->plaintext; // Find the first TD (starts with 0)
       $password= $tr->find('td', 1)->plaintext; // Find the second TD (which will be 1)
        echo "INSERT INTO users (id, username, password) VALUES (NULL, '$username', '$password') <br />"; // Do your insert query here!
    }
    

输出(我对此进行了测试):

INSERT INTO users (id, username, password) VALUES (NULL, '', '') 
INSERT INTO users (id, username, password) VALUES (NULL, 'User_1', 'Password_1') 
INSERT INTO users (id, username, password) VALUES (NULL, 'User_2', 'Password_2') 

所以从 html 文件中删除第一个 &lt;tr&gt;,因为它是表格标题。

【讨论】:

  • 马上试试这个,谢谢!
  • 刚试了一下,抱歉花了我很多时间,不得不做81列。我收到致命错误:调用非对象上的成员函数 find()
  • 你是否包含了简单的 html dom 类?
  • 我必须编辑 MAX_FILE_SIZE,它现在正在运行,会回复结果。
  • 啊,那张桌子一定很大!
猜你喜欢
  • 2012-07-11
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 2016-06-21
  • 1970-01-01
相关资源
最近更新 更多