问题描述
我一直在尝试使用pdo将一些标签插入表中,但无济于事.
I've been trying to insert some tags into a table using pdo but to no avail.
我有一个名为Tag的php数组.
I have a php array called Tag.
标签数组中的样本数据如下
Sample data in tag array is as follows
tag[] = [[a,b,c,d,e],[f,g,h,i,j]]
使用下面的for循环将其转换为(1,'a','b','c','e',0),(1,'f','g',' h','i',0)
using a for loop below I'm able to convert it to (1,'a','b','c','e',0), (1,'f','g','h','i',0)
$value="";
$value .= "($postid,";
for($i=0;$i<sizeof($tag);$i++)
{
$value .="'$tag[$i]'";
if($i + 1 == $sizeof($tag){
$value .=")";
}else{
$value .="),";
}
}
并按如下所示准备并插入表格中
And prepare and insert into the table as follows
$inserttagquery = "insert Into tagtable ( postid, desc, b, u, toppos,leftpos ,ver) values :value";
$queryinserttag = $conn->prepare($inserttagquery);
$queryinserttag->execute(array('value'=>$value));
$insertedtag = $queryinserttag->rowCount();
但是,这似乎不起作用. $ insertedtag不返回任何值.
However, this does not seem to work. $insertedtag does not return any value.
推荐答案
您的SQL完全错误.您在字段列表中添加了6个字段 AND ,然后仅提供一个占位符来为这些字段提供值.
Your SQL is outright wrong. You're lising 6 fields AND a constant value in your field list, then providing only a SINGLE placeholder to provide values for those fields.
- 您不能使用数字作为字段名称.
0
是完全语法错误和无效的字段名称. - 占位符在字段和值之间具有1:1关系.您不能将多个值推送到一个变量中,并尝试将其与占位符一起使用以填写其他字段.
- you cannot use numbers as a field name.
0
is a flat out syntax error and an invalid field name. - Placeholders have a 1:1 relation between a field and a value. You CANNOT shove multiple values into a single variable and try to use that value with a placeholder to fill in OTHER fields.
您的查询应该是:
INSERT INTO tagtable (postid, desc, b, u, etc...)
VALUES (:postid, :desc, :b, :u, etc...)
,然后为每个保镖提供INDIVIDUAL值:
and then you provide INDIVIDUAL values for each placehodler:
$stmt->execute(array($postid, $desc, $b, $u, etc...));
按照所写,并忽略所有其他问题,您的查询将尝试将(1, 'f', 'g', etc..)
字符串推入postid
字段中.
As written, and ignoring all the other problems, your query would try shove your (1, 'f', 'g', etc..)
string into JUST the postid
field.
这篇关于PDO json插入多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!