0
我有以下列表與< SPAN>標籤內部意見</SPAN>PHP:獲取HTML標籤中的內容和INSERT表
我如何保存到我的表中的每個有何評論?
<ul id="list">
<li>
<span>Comment 1</span>
</li>
<li>
<span>Comment 2</span>
</li>
<li>
<span>Comment 3</span>
</li>
</ul>
我想將它保存到我的表是這樣的:
+----+--------+------------+
| id | time | comment |
+----+--------+------------+
| 1 | 12:05 | Comment 1 |
+----+--------+------------+
| 2 | 14:20 | Comment 2 |
+----+--------+------------+
| 3 | 17:41 | Comment 3 |
+----+--------+------------+
我當前的PHP代碼:
<?php
$connect = mysqli_connect("localhost","daniel","password","comments_db");
$time = time();
// $comments = ??????;
foreach($comments as $comment) {
mysqli_query($connect,"INSERT INTO comments VALUES ('".$time."', '".$comment."')");
}
?>
我怎樣才能做到這一點?提前致謝。
當使用'mysqli'你應該使用參數化查詢和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值來實現此目的,因爲您將創建嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 – tadman
評論如何產生?您需要採取什麼行動來使插入發生? – asprin
如果你想解析HTML中的PHP有[這個答案](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php )。 – tadman