简体   繁体   English

php + mysql问题(字段名“ group”)

[英]problem with php + mysql ( field name “group” )

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 问题:好的,我有一个函数可以帮助我简化插入语句(pdo),问题是当我在其中插入“组”名称时插入字段

$a['group'] = $_POST['group'];
$a['tag'] = $_POST['tag'];
$a['information'] = $_POST['information'];
$status= $this->insert ( 'groups', $a);

it will generate a mysql error like 它将产生一个mysql错误,如

PDOStatement::execute() [pdostatement.execute]: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; PDOStatement :: execute()[pdostatement.execute]:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请参见参考。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'group ,tag ,information ) VALUES ('agroup', 'atag', 'ainformation' )' 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'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 亚当·拉玛丹(Adam Ramadhan)

Quote group with backticks. 带引号的报价group

Example

`group`

group is a reserved word in SQL. group是SQL中的保留字。 You may have used... 您可能已经使用过...

GROUP BY `id` DESC

...or similar before. ...或之前类似。

If using >= PHP 5.3... 如果使用> = PHP 5.3 ...

$fieldnames = array_map(function($field) {
    return "`$field`";
}, $fieldnames);

Where you have: 您在哪里:

  INSERT INTO groups( "group" ,"tag" ,"information" ) ...

You need to have: 您需要具备:

  INSERT INTO `groups` ( `group` ,`tag` ,`information` ) ...

Do this with the following: 执行以下操作:

  $groups = array('group','tag','information');
  $mysql_groups = '`' . implode('`,`', $groups) . '`';
  $sql = 'INSERT INTO `groups` (' . $mysql_groups . ') ...';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM