HTML form input field with comma separated

前端 未结 4 1194
梦毁少年i
梦毁少年i 2021-01-23 22:38

I am trying to figure out how to take multiple word input in a single text box in a form with each word separated by comma(,) and then paring the word based on comma and inserti

相关标签:
4条回答
  • 2021-01-23 23:15

    You could do it with PDO too:

    <?php 
    //Connect safely to your database
    try {
        $db = new PDO("mysql:host=localhost;dbname=test", 'root', 'password');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        die('Cannot connect to mySQL server. Details:'.$e->getMessage());
    }
    
    
    if ($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['words'])) {
    
        $sql = "INSERT INTO words_table (word) VALUES (:word)";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':word', $word);
    
        foreach (explode(',', $_POST['words']) as $word) {
            $word = trim($word);
            if (empty($word)) {
                continue;
            }
            $stmt->execute();
        }
    }
    //Your form
    ?>
    <h1>Words</h1>
    <form method="POST" action="">
        <input type="text" name="words"/>
        <input type="submit" name="submit" value="Submit"/>
    </form>
    
    0 讨论(0)
  • 2021-01-23 23:17

    I know a bunch of mysql_* function answers are going to come in, so ill add the prepared query route.

    This isn't flawless, but you'll get the idea

    // Get the array of words
    $words = explode( ',', $formname );
    
    // Create an array of :word1, :word2, :word3, etc for use in binding
    foreach( range( 1, count( $words ) ) as $wordnumber ) {
        $bind[] = ':word'.$wordnumber;
    }
    
    // Create the sql query
    $sql = sprintf( "insert into table ( word ) values ( %s )", implode( '),(', $bind ) );
    
    // Prepare the query
    $stmnt = $pdo->prepare( $sql );
    
    // Bind each word
    foreach( $words as $k => $word ) {
        $stmnt->bindValue( ":word" . ++$k, $word );
    }
    
    // Execute
    $stmnt->execute();
    
    0 讨论(0)
  • 2021-01-23 23:26

    If you need to encapsulate the words in quotes, this should do the trick:

    <?php
    
    $myString='sometext,someothertext,and something else';
    $query="insert into table1 (columnName) values (('".str_replace(',',"'),('",$myString)."'))";
    
    echo $query;
    ?>
    

    Output:

    insert into table1 (columnName) values (('sometext'),('someothertext'),('and something else')) 
    

    This will insert multiple records in proper accordance with the mysql insert multiple values syntax.

    0 讨论(0)
  • 2021-01-23 23:29
    $str = $_POST['words'];
    $piece = explode(',',$str);
    foreach($piece as $substr){
        mysql_query("INSERT INTO test (words) VALUES ('".$substr."');";
    }
    
    0 讨论(0)
提交回复
热议问题