dqyanc7264
2015-08-10 17:01
浏览 64
已采纳

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

<html>
<form method="POST" action="insertlines.php">
<textarea name="url" rows="10" ></textarea>
<input type="submit" name="submit" value="Enter">
</form>
</html>

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 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doufulian4076 2015-08-10 17:48
    已采纳

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

    <?php
    if(isset($_POST['url'])){
        if(strpos($_POST['url'], "
    ")){
            $entries = explode("
    ", $_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
    }
    ?>
    
    已采纳该答案
    打赏 评论
  • doutou19761022 2015-08-10 17:48

    Try this:

    $textarea=$_POST['url']
    $sql = 'INSERT INTO YourTable(field1,field2,name) VALUES';
    foreach(explode("
    ", $textarea) as $row) {
    $sql.='("field1","field2",.'$row'.)';
    } 
    $conn->query($sql)
    
    打赏 评论

相关推荐 更多相似问题