Insert multiple records of a form

0

I'm trying to make a multiple insert into the database via $ _POST but the result is not as expected

    <form name="formulario" method="post" enctype="multipart/form-data" action="">

    <input name="texto" type="text"/>

    <input type="submit" value="enviar"/>

    </form>

    <?php
    if(isset($_POST)){

    $texto = $_POST['texto'];

        $ext = implode(",",$texto);
        var_dump($ext);

        /*resultado da impressão:
        string 'texto1,texto2,texto3' (length=20)
        */

                $insert = DB::getConn()->prepare('INSERT INTO 'table' SET 'texto'=?');
                return $insert->execute(array($ext)) ? 1 : 0;

        /*No Banco de dados mysql resultou 

         id|1|texto|texto1,texto2,texto3

         Eu gostaria que cada palavra fosse inserida em uma coluna:

         id|1|texto|texto1
         id|2|texto|texto2
         id|3|texto|texto3
        */
    }
    
asked by anonymous 29.04.2016 / 06:18

2 answers

0

I would particularly elaborate the solution as follows:

<form name="formulario" method="post" enctype="multipart/form-data" action="">
    <input name="texto" type="text"/>
    <input type="submit" value="enviar"/>
</form>

<?php
if(isset($_POST)){

    $texto = $_POST['texto'];

    $ext = implode(",",$texto);

    for($i=0; $i<=count($ext); $i++){
        $insert = DB::getConn()->prepare("INSERT INTO table (texto='".$ext[$i]."')");
        return $insert->execute(array($ext)) ? 1 : 0;
    }
}
?>

Note that I used for() to count how many array() has within $ext , so yes insert . I hope I have helped!

    
29.04.2016 / 13:27
0

First: Your SQL is incorrect in executing the script, if you are inserting into the database the correct one would be

INSERT INTO TABELA (COLUM1, COLUM2) VALUES (VALUE1, VALUE2).

Second point: If you want to do a multi-record insert, you must have a repeat loop with the stop condition you set.

This way I've seen your form I'm not understanding how you want to insert multiple records.

    
12.12.2016 / 11:23