插入阵列数据为使用MySQL的PDO [英] insert array data into mysql using PDO
本文介绍了插入阵列数据为使用MySQL的PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个阵列从一个HTML表单数据,我要这个数组被插入到处理插入
查询到mysql PHP函数。
我怎么可以声明我的数组PDO
如果(使用isset($ _ POST ['提交'])); {
$ _ POST ['名'];
$ _ POST ['年龄'];
$ myArray的=阵列();
的foreach($作为重点=&GT $ _ POST,$值){
$ myArray的[$关键] = $价值;
}无论($ myarray的);}无论功能($ myArray的){
$ SQL = $ DB-GT&; prepare(INSERT INTO`user`(`name`,`age`)VALUES(:姓名,:年龄)); 的foreach($ myarray中为$行=> $值){
SQL-$> bindValue(数组($ myArray的)){
}
SQL-$>执行();
}
道歉忘记错误。
这是我所得到的
警告:PDOStatement对象:: bindValue()预计,至少有2个参数,1用C给出:\\网络\\ XAMPP \\ htdocs中\\在线36 submit.php 警告:PDOStatement对象:: bindValue()预计,至少有2个参数,1用C给出:\\网络\\ XAMPP \\ htdocs中\\在线36 submit.php 警告:PDOStatement对象:: bindValue()预计,至少有2个参数,1用C给出:\\网络\\ XAMPP \\ htdocs中\\在线36 submit.php 致命错误:未捕获的异常'PDOException有消息SQLSTATE [HY093]:无效的参数编号:没有参数被束缚在
C:\\网络\\ XAMPP \\ htdocs中\\ submit.php:38堆栈跟踪:0#
C:\\网络\\ XAMPP \\ htdocs中\\ submit.php(38):PDOStatement->的execute()#1
C:\\网络\\ XAMPP \\ htdocs中\\ submit.php(12):寄存器(阵列)#2主{}抛出
C:\\网络\\ XAMPP \\ htdocs中\\在线38 submit.php
解决方案
函数pdoSet($领域,和放大器; $值$源=阵列()){
$设置='';
$值=阵列();
如果(!$源)$源=安培; $ _ POST;
的foreach($领域$场){
如果(使用isset($源[$场])){
$设置。=`.str_replace(`,``,$场)。`。 =:$场;
$值[$场] = $源[$场]
}
}
返回SUBSTR($集,0,-2);
}
此功能会产生一个正确的顺序为SET运营商,
`field1` =:FIELD1,`field2` =:场
要插入查询和avtual数据值存储在 $值
阵列的execute()
。
$栏=阵列('身份证','名','年龄','禄'); //允许的领域
$的SQL =INSERT INTO`user` SET.pdoSet($领域,$值);
$ STM = $ dbh-> prepare($的SQL);
$ STM-GT&;执行($值);
i have this array with data from a html form, and i want this array to be inserted into PHP function that handles the INSERT
query into mysql.
how can i declare my array into PDO
if (isset($_POST['submit'])); {
$_POST['name'];
$_POST['age'];
$myarray= array();
foreach ($_POST as $key => $value){
$myarray[$key] = $value;
}
whatever($myarray);
}
functions whatever($myarray) {
$sql=$db->prepare("INSERT INTO `user`(`name`, `age`) VALUES (:name,:age)");
foreach($myarray as $row=>$value){
$sql->bindValue(array($myarray)){
}
$sql->execute();
}
apology for forgetting the error.
this is what i get
Warning: PDOStatement::bindValue() expects at least 2 parameters, 1 given in C:\Web\xampp\htdocs\submit.php on line 36
Warning: PDOStatement::bindValue() expects at least 2 parameters, 1 given in C:\Web\xampp\htdocs\submit.php on line 36
Warning: PDOStatement::bindValue() expects at least 2 parameters, 1 given in C:\Web\xampp\htdocs\submit.php on line 36
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound' in
C:\Web\xampp\htdocs\submit.php:38 Stack trace: #0
C:\Web\xampp\htdocs\submit.php(38): PDOStatement->execute() #1
C:\Web\xampp\htdocs\submit.php(12): register(Array) #2 {main} thrown in
C:\Web\xampp\htdocs\submit.php on line 38
解决方案
function pdoSet($fields, &$values, $source = array()) {
$set = '';
$values = array();
if (!$source) $source = &$_POST;
foreach ($fields as $field) {
if (isset($source[$field])) {
$set.="`".str_replace("`","``",$field)."`". "=:$field, ";
$values[$field] = $source[$field];
}
}
return substr($set, 0, -2);
}
This function will produce a correct sequence for the SET operator,
`field1`=:field1,`field2`=:field2
to be inserted into query and store avtual data values in $values
array for execute()
.
$fields = array('id','name','age','loc'); // allowed fields
$sql = "INSERT INTO `user` SET".pdoSet($fields,$values);
$stm = $dbh->prepare($sql);
$stm->execute($values);
这篇关于插入阵列数据为使用MySQL的PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文