php - PDO 阵列插入

标签 php mysql arrays pdo

<分区>

所以数组按照我想要的方式出现了 数组的任何示例,其大小可能会有所不同,并且下面有类似 ' <\/的内容

$TablesNames
Array 
( 
   [0] => SampleDate 
   [1] => LAB 
) 

$LineResults
Array 
( 
  [0] => 4/08/2014 
  [1] => Micro - Water 
)

我尝试插入值的代码摘要

$sqlTableNames = (implode(',',$TableNames));

for ($x=0; $x<$Xsize; $x++) 
{
  for($y=0;$y<$MapSize;$y++)
  {     
        $LineResults[$x][$y] = $results[$x][$map[$y]];          
  }
$sqlLineResults = (implode("','",$LineResults[$x]));

$ResultsInsert = $db->prepare("INSERT INTO samples (:TableValues) VALUES (:LineValues)");   
$ResultsInsert->bindParam(':TableValues', $sqlTableNames, PDO::PARAM_STR);
$ResultsInsert->bindParam(':LineValues', $sqlLineResults, PDO::PARAM_INT);  
$ResultsInsert->execute();

}

抛出错误“...在第 1 行的 '?) VALUES (?)' 附近使用正确的语法 ...”

最佳答案

这里有两个问题:

  • 您不能绑定(bind)表名或列名,只能绑定(bind)值;
  • 您只能绑定(bind)单个值,不能绑定(bind)多个值的字符串。

所以 VALUES 子句的两边都是错误的。

您将需要动态构建 sql 语句,分别添加键值对(或两侧的字符串)。由于您无法绑定(bind)表名和列名,因此您应该使用白名单以防输入来自访问者。

关于php - PDO 阵列插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26288863/

相关文章:

php - 字段列表中的未知列。

php - jQuery 自定义模板无法正常工作

javascript - 无法在 javascript jQuery 中读取 PHP 结构数组

php - 上传图片到MySQL数据库

php - 在数据库表中搜索电话号码

c - 从以空格分隔的文件中读取整数

MySQL 查询 : Using group by and getting unreal results

mysql - MySQL 查询中的正则表达式错误

c - 在结构数组中输入字符的循环无法正常工作

javascript - 从 javascript 数组中删除索引 altogehter