Php 我得到的sql语法错误和力匹配的任何建议的答案

Php 我得到的sql语法错误和力匹配的任何建议的答案,php,cakephp,Php,Cakephp,错误:SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在“dashboards”(仪表盘)(使用id、系统id、on-time、off-time、is-active)值('Array'在第1行)附近使用的正确语法 public function admin_dashboard($id=null) { $this->loadModel('Employ','System'); if

错误:SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在“dashboards”(仪表盘)(使用id、系统id、on-time、off-time、is-active)值('Array'在第1行)附近使用的正确语法

public function admin_dashboard($id=null) {
        $this->loadModel('Employ','System');
        if ($this->request->is('post')) {
            //debug($this->request->data);exit;
            $employId = $this->request->data['Dashboard']['employ_id'];
            $systemId = $this->request->data['Dashboard']['system_id'];
            $logon = $this->request->data['Dashboard']['on-time'] = date("Y-m-d H:i:s");
            $logout = $this->request->data['Dashboard']['off-time'] = date("Y-m-d H:i:s");
            $active = $this->request->data['Dashboard']['is_active'];

            //$date=date("Y-m-d H:i:s");
            for($i=0;$i<count ($systemId);$i++){


            $this->System->query("insert into 'dashboards' (employ_id,system_id,on-time,off-time,is_active) values              ('$employId','$systemId[$i]','$logon','$logout','$active')");
            //$var_dump($date);
            }

            $this->Session->setFlash(__('The query has been saved.'));
                return $this->redirect(array('action' => 'admin_dashboard'));

        }/* else {
                $this->Session->setFlash(__('The query could not be saved. Please, try again.'));
            }*/
        $employs = $this->Employ->find('list');
        $systems = $this->System->find('list');
        $this->set(compact('employs','systems'));
    }
公共功能管理仪表板($id=null){
$this->loadModel('Employ','System');
如果($this->request->is('post')){
//调试($this->request->data);退出;
$employId=$this->request->data['Dashboard']['employee_id'];
$systemId=$this->request->data['Dashboard']['system_id'];
$logon=$this->request->data['Dashboard']['on-time']=date(“Y-m-dh:i:s”);
$logout=$this->request->data['Dashboard']['off-time']=date(“Y-m-dh:i:s”);
$active=$this->request->data['Dashboard']['is_active'];
//$date=日期(“Y-m-d H:i:s”);
对于($i=0;$iSystem->query($i=0;$iSystem->query)(“插入到‘仪表板’(仪表板’(仪表板id、系统id、开启时间、关闭时间、处于活动状态))值(“$employId’、“$systemId[$i]”、“$logon’、“$logout’、“$active”);
//$var_dump($date);
}
$this->Session->setFlash(_u('查询已保存');
返回$this->redirect(数组('action'=>'admin_dashboard');
}/*否则{
$this->Session->setFlash(_u('无法保存查询。请重试');
}*/
$employers=$this->employer->find('list');
$systems=$this->System->find('list');
$this->set(compact('employees','systems');
}
MySQL标识符引号字符是反勾号 问题中的第一个错误是因为反勾号不是一个引号

i、 e.有效期:

mysql> SELECT * FROM `select` WHERE `select`.id > 100;
无效:

mysql> SELECT * FROM 'select' WHERE 'select'.id > 100;
ERROR 1064 (42000): 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 ''select' WHERE 'select'.id > 100' at line 1
mysql> 
确实存在堆栈溢出,因为这是一个常见的错误

不需要使用查询 但是,这不是问题代码中最大的问题/错误。设计/预期用于:

不能或不希望通过其他模型方法进行的SQL调用

将数据插入数据库并不属于这一类;实际上,它应该作为最后手段使用(一般来说,很少需要使用这种方法)

与此类似的东西是所有需要的:

$this->loadModel('Dashboard');
$data = [];
foreach ($this->request->data['Dashboard']['system_id'] as $systemId) {
    $row = $this->request->data['Dashboard'];
    $row['system_id'] = $systemId;
    $data[] = $row;
}
$this->Dashboard->saveMany($data);
是问题中的一个,也是最合适的信息

请注意,如果逻辑超过几行代码,则它根本不应处于控制器操作中,而应移到模型中,例如:

// in controller
$this->Dashboard->myMethod($data);

// in the Dashboard class:
public function myMethod($input)
{
    ... whatever is necessary ...
    return $this->saveMany($data);
}

删除“dashboard”中的引号为什么要使用
query
呢?如果不使用query,我如何将值插入到依赖于system controller的dashboard中。如果你可以选择suugest me,Vickrant也尝试了你的方法bt,它根本不获取输入看起来你真的应该阅读有关的基本知识。