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