HTML form input field with comma separated

前端 未结 4 1197
梦毁少年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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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();
    

提交回复
热议问题