如何在MySQL中从文本区域插入多行 [英] How to insert multiple rows from a textarea in MySQL
本文介绍了如何在MySQL中从文本区域插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<html>
<form method="POST" action="insertlines.php">
<textarea name="url" rows="10" ></textarea>
<input type="submit" name="submit" value="Enter">
</form>
</html>
如何将文本区域的每一行放入MySQL行?
How can i put every single row of the textarea into a MySQL row ?
我想要的是输入以下内容:
The thing I want is when I input:
John
Peter
Steven
在文本区域中,我希望它们在数据库中分别具有不同的ID.
in the textarea, I want them in my database with different ids each.
推荐答案
您必须解析文本,寻找"enter"字符:
You have to parse the text, looking for the "enter" character:
<?php
if(isset($_POST['url'])){
if(strpos($_POST['url'], "\n")){
$entries = explode("\n", $_POST['url']);
} else {
$entries = array($_POST['url']);
}
// connect to DB here
// then iterate over entries
foreach($entries as $e){
// build some type of Prepared Statement to protect from SQL Injection
$q = "INSERT INTO table (col1) VALUES (?)";
// bind $e to statements
// Execute SQL statements
}
// close DB connection
}
?>
这篇关于如何在MySQL中从文本区域插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文