普通网友 2015-08-10 09:01
浏览 65
已采纳

如何在MySQL中的textarea中插入多行

  1. <html>
  2. <form method="POST" action="insertlines.php">
  3. <textarea name="url" rows="10" ></textarea>
  4. <input type="submit" name="submit" value="Enter">
  5. </form>
  6. </html>

How can i put every single row of the textarea into a MySQL row ?

The thing I want is when I input:

  1. John
  2. Peter
  3. Steven

in the textarea, I want them in my database with different ids each.

  • 写回答

2条回答 默认 最新

  • doufulian4076 2015-08-10 09:48
    关注

    You have to parse the text, looking for the "enter" character:

    1. <?php
    2. if(isset($_POST['url'])){
    3. if(strpos($_POST['url'], "
    4. ")){
    5. $entries = explode("
    6. ", $_POST['url']);
    7. } else {
    8. $entries = array($_POST['url']);
    9. }
    10. // connect to DB here
    11. // then iterate over entries
    12. foreach($entries as $e){
    13. // build some type of Prepared Statement to protect from SQL Injection
    14. $q = "INSERT INTO table (col1) VALUES (?)";
    15. // bind $e to statements
    16. // Execute SQL statements
    17. }
    18. // close DB connection
    19. }
    20. ?>
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部