HTML form input field with comma separated

前端 未结 4 1235
梦毁少年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
    2021-01-23 23:15

    You could do it with PDO too:

    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
    ?>
    

    Words

提交回复
热议问题