HTML& PHPMySQL,从textarea将许多数据插入到Table中 [英] HTML & PHPMySQL , Insert many datas into Table from textarea
问题描述
你好,我试图从一个文本框(文本区域)插入数据,但数据应该以行 FIELDS TERMINATED BY'\\\
'
我的HTML是
< form method =getid =testformid action =upload-datas.php>
< input type =submit/>
< / form>
现在,如果我在这个textare数据中写入如
1
22
333
如何让我的 upload-datas.php 终止从顶部表单获取的数据?
Ps这就像CVS文件,但我喜欢从textarea插入数据!
这应该就足够了:
- 正如RiggsFolly提到的那样,应该将 textarea 移动到 b $ b
-
我会将表单提交方法 POST 而不是 GET 。这样数据将不会被附加到URL(即在查询字符串中)。
< form method =postid =testformid>
-
使用双引号分隔新行字符(即
\\\
):
eg$ lines = explode(\\\
,$ _ POST ['taname']); 使用mysqli函数(例如 mysqli_connect(), mysqli_prepare(), mysqli_bind_param()和 mysqli_execute() a>)或 PDO功能将数据插入数据库中
编辑:
调用 mysqli_bind_param()必须一次调用所有参数,而不是每个参数调用一次。为了用不同数量的参数来实现这一点,我们必须使用类似于本文。
<?php
$ lines = array();
$ output ='';
if(array_key_exists('taname',$ _ POST)&& $ _POST ['taname']){
$ lines = explode(\\\
,$ _ POST ['taname']] );
//在此
// $ host ='localhost'; ... etc ...
$连接前定义$ host,$ username,$ pw,$ databaseName = mysqli_connect($ host,$ username,$ pw,$ databaseName);
if($ connection){
$ paramHolders = array_map(function(){return'?';},$ lines);
//更新表名,相应的列名称
$ insertQuery ='INSERT INTO tableName(columnName)VALUES('.implode('),(',$ paramHolders)。')';
$ statement = mysqli_prepare($ connection,$ insertQuery);
//这将用作mysql_stmt_bind_param的第一个参数
//例如'ssss' - 's'为每行
$ types = str_repeat('s',count($ lines));
$ params = array($ statement,& $ types);
//通过引用参数列表添加每行
foreach($ lines as $ index => $ line){
$ output。='< div>插入的行:'。$ line。'< / div>';
$ params [] =& $ lines [$ index];
}
//以不同数量的参数调用mysql_bind_param()
call_user_func_array('mysqli_stmt_bind_param',$ params);
$ statement-> execute();
}
}
?>
< html>
< body>
< form method =postid =testformid>
< textarea name =tanameid =taidcols =35wrap =soft>< / textarea>
< input type =submit/>
< / form>
<? echo $ output; ?>
< / body>
< / html>
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>
这篇关于HTML& PHPMySQL,从textarea将许多数据插入到Table中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!