php 的问题 + mysql(字段名称“组”)
public function insert($table, $data = array()) {
$fieldnames = array_keys ( $data );
var_dump(implode ( ' ,', $fieldnames ));
$name = '( ' . implode ( ' ,', $fieldnames ) . ' )';
$value = '(:' . implode ( ', :', $fieldnames ) . ' )';
$query = "INSERT INTO $table";
$query .= $name . ' VALUES ' . $value;
var_dump($query);
$insert = $this->start->prepare ( $query );
return $insert->execute ( $data );
}
问题:好的,我有一个函数可以帮助我简化插入语句(pdo),问题是当我插入一个字段时,如果上面有“组”名称,
$a['group'] = $_POST['group'];
$a['tag'] = $_POST['tag'];
$a['information'] = $_POST['information'];
$status= $this->insert ( 'groups', $a);
它会生成一个 mysql 错误,例如
PDO语句::execute() [pdostatement.execute]: SQLSTATE[42000]:语法错误或 访问冲突:1064 您有一个 SQL 语法错误;检查 与您的 MySQL 对应的手册 服务器版本的正确语法 使用靠近“组、标签、信息”) VALUES ('agroup', 'atag', '信息')'
查询
INSERT INTO groups( group ,tag ,information ) VALUES (:group, :tag, :information )
这是我猜测的
INSERT INTO groups( "group" ,"tag" ,"information" ) VALUES (:group, :tag, :information )
,我不确定我们应该从哪里开始。
感谢您关注
亚当·拉马丹
public function insert($table, $data = array()) {
$fieldnames = array_keys ( $data );
var_dump(implode ( ' ,', $fieldnames ));
$name = '( ' . implode ( ' ,', $fieldnames ) . ' )';
$value = '(:' . implode ( ', :', $fieldnames ) . ' )';
$query = "INSERT INTO $table";
$query .= $name . ' VALUES ' . $value;
var_dump($query);
$insert = $this->start->prepare ( $query );
return $insert->execute ( $data );
}
Question: ok i have a function that helps me simplify the insert statement (pdo) the problem is is when i insert a field if "group" name on it
$a['group'] = $_POST['group'];
$a['tag'] = $_POST['tag'];
$a['information'] = $_POST['information'];
$status= $this->insert ( 'groups', $a);
it will generate a mysql error like
PDOStatement::execute()
[pdostatement.execute]:
SQLSTATE[42000]: Syntax error or
access violation: 1064 You have an
error in your SQL syntax; check the
manual that corresponds to your MySQL
server version for the right syntax to
use near 'group ,tag ,information )
VALUES ('agroup', 'atag',
'ainformation' )'
this is the query
INSERT INTO groups( group ,tag ,information ) VALUES (:group, :tag, :information )
im guessing that it should be
INSERT INTO groups( "group" ,"tag" ,"information" ) VALUES (:group, :tag, :information )
im not sure where should we start from.
Thanks for looking in
Adam Ramadhan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用反引号引用
group
。示例
group
是 SQL 中的保留字。您以前可能使用过......或类似的东西。
如果使用 >= PHP 5.3...
Quote
group
with backticks.Example
group
is a reserved word in SQL. You may have used......or similar before.
If using >= PHP 5.3...
您拥有:
您需要:
通过以下操作执行此操作:
Where you have:
You need to have:
Do this with the following: