<html>
<form method="POST" action="insertlines.php">
<textarea name="url" rows="10" ></textarea>
<input type="submit" name="submit" value="Enter">
</form>
</html>
如何将文本区域的每一行放入 MySQL 行中?
我想要的是当我输入:
John
Peter
Steven
在文本区域中,我希望它们在我的数据库中具有不同的 ID。
最佳答案
您必须解析文本,寻找“enter”字符:
<?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
}
?>
关于php - 如何从 MySQL 中的文本区域插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925222/