0

Hi I'm trying to insert datas from one textbox (Text Area) but datas should be terminated by lines FIELDS TERMINATED BY '\n'

My HTML is

    <form method="get" id="testformid" action="upload-datas.php">
    <input type="submit" />
</form> 
<textarea form ="testformid" name="taname" id="taid" cols="35" wrap="soft"></textarea>

Now if I write in this textare datas like

1
22
333

How can I make my upload-datas.php to terminate datas which it gets from the form on top? P.s this is something like CVS file but I like to make it inserting data from textarea!

A.Krasniqi
  • 165
  • 12
  • 1
    All `` tags must be INSIDE the `
    ...
    ` tags or they are ignored
    – RiggsFolly Jan 02 '17 at 16:03
  • how you can see there's no '' outside the '
    ...
    '
    – A.Krasniqi Jan 02 '17 at 16:05
  • 1
    And of course ` – RiggsFolly Jan 02 '17 at 16:11
  • @RiggsFolly Okay now how should php be to get datas from the form then inserting them terminated by "\n"? – A.Krasniqi Jan 02 '17 at 17:18
  • I'm actually having troubles understanding your question. Can you give me an example of what you want to get when you submit the form? – Gynteniuxas Jan 02 '17 at 17:24
  • @EdvinTenovimas My PHP Insert the datas into the Table, but it doesn't terminate it! Datas in not splitted so I want to explode the data automatically from PHP and inserting into table as a row. For every new line, in the table it should be a new row, of course in the same column – A.Krasniqi Jan 02 '17 at 17:41

1 Answers1

0

A couple changes that should suffice:

  • As RiggsFolly mentioned, the textarea should be moved into the form
  • I would make the form submit method POST instead of GET. That way the data won't be appended to the URL (i.e. in the query string).

    <form method="post" id="testformid">

  • Delimit the new line character (i.e. \n) using double quotes: e.g. $lines = explode("\n",$_POST['taname']);

  • Use mysqli functions (e.g. mysqli_connect(), mysqli_prepare(), mysqli_bind_param() and mysqli_execute()) or PDO functions to insert the data into the database

Edit:

The call to mysqli_bind_param() must be called with all parameters at once, instead of once per parameter. In order to do this with a varying number of parameters, we must use a technique like the one described in this article.

    <?php
    $lines = array();
    $output = '';
    if(array_key_exists('taname',$_POST) && $_POST['taname']) {
        $lines = explode("\n",$_POST['taname']);

        //define $host, $username, $pw, $databaseName before this
        //$host = 'localhost';...etc...
        $connection = mysqli_connect($host,$username, $pw, $databaseName);
        if ($connection) {
            $paramHolders = array_map(function() { return '?';},$lines);

            //update tablename, column name accordingly 
            $insertQuery = 'INSERT INTO tableName (columnName) VALUES ('.implode('), (',$paramHolders).')';
            $statement = mysqli_prepare($connection,$insertQuery);
            //this will be used as the first param to mysql_stmt_bind_param
            // e.g. 'ssss' - one 's' for each line
            $types = str_repeat('s',count($lines));
            $params = array($statement,&$types);
            //add each line by-reference to the list of parameters
            foreach($lines as $index=>$line) {
                $output .= '<div>inserted line: '.$line.'</div>';
                $params[] = &$lines[$index];
            }
            //call mysql_bind_param() with the varying number of arguments
            call_user_func_array('mysqli_stmt_bind_param',$params);
            $statement->execute();
        }
    }
    ?>
    <html>
        <body>
            <form method="post" id="testformid">
                <textarea name="taname" id="taid" cols="35" wrap="soft"></textarea>
                <input type="submit"/>
            </form>
            <? echo $output; ?>
        </body>
    </html>
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • Now how to make it to insert the datas into MySQL Database localhost , root , password, database_name – A.Krasniqi Jan 02 '17 at 20:14
  • it gives to me : Uncaught Error: Call to undefined method mysqli_stmt::bindParam() in – A.Krasniqi Jan 02 '17 at 21:00
  • Sorry I mixed up [procedural style](http://php.net/manual/en/mysqli-stmt.bind-param.php#example-1849) with [object-oriented style](http://php.net/manual/en/mysqli-stmt.bind-param.php#example-1848) - I updated the sample code – Sᴀᴍ Onᴇᴌᴀ Jan 02 '17 at 21:01
  • Now when I type more than 1 line at textarea box it gives : mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement in ... – A.Krasniqi Jan 02 '17 at 21:06
  • see my updated answer - it allows varying parameters with the call to mysqli_bind_param() – Sᴀᴍ Onᴇᴌᴀ Jan 02 '17 at 22:57