2014-09-05 166 views
0

我在添加用户时遇到此错误。Cake php SQLSTATE [42000]:语法错误或访问冲突:1064

我有一个视图adduser.ctp

和在用户控制器我有这样的功能现在

public function addUser() { 
    $userGroups = $this->UserGroup->getGroups(); 
    unset($userGroups[1]); 
    unset($userGroups[2]); 
    $this->set('userGroups', $userGroups); 
    if ($this->request->isPost()) { 
     $this->User->set($this->data); 
     if ($this->User->AddEditValidate()) { 
      $this->request->data['User']['email_verified'] = 1; 
      $this->request->data['User']['active'] = 2; 
      $this->request->data['User']['parent_id'] = $this->Session->read('logged_client_id'); 
      $salt = $this->UserAuth->makeSalt(); 
      $this->request->data['User']['salt'] = $salt; 
      $this->request->data['User']['temp_password'] = $this->request->data['User']['password']; 
      $this->request->data['User']['password'] = $this->UserAuth->makePassword($this->request->data['User']['password'], $salt); 
      if ($this->User->save($this->request->data, false)) { 
       $this->sendLoginEmail($this->request->data['User']); 
       $this->Session->setFlash(__('The user is successfully added'), 'success_flash'); 
       $this->redirect('/clientUsers/' . $this->Session->read('logged_client_id')); 
      } else { 
       $this->Session->setFlash(__('Problem in saving user information'), 'error_flash'); 
      } 
     } 
    } 
} 

在用户模型予由以下函数

function AddEditValidate() { 
    $validate1 = array(
     "user_group_id" => array(
      'rule' => array('comparison', '!=', 0), 
      'message' => 'Please select group'), 
     'first_name' => array(
      'mustNotEmpty' => array(
       'rule' => 'notEmpty', 
       'message' => 'Please enter first name'), 
      'custom' => array(
       'rule' => 'alphaNumericSpace', 
       'message' => 'First name should be alphnumeric') 
     ), 
     'last_name' => array(
      'mustNotEmpty' => array(
       'rule' => 'notEmpty', 
       'on' => 'create', 
       'message' => 'Please enter last name'), 
      'custom' => array(
       'rule' => 'alphaNumericSpace', 
       'message' => 'Last name should be alphnumeric') 
     ), 
     'email' => array(
      'mustNotEmpty' => array(
       'rule' => 'notEmpty', 
       'message' => 'Please enter email', 
       'last' => true), 
      'mustBeEmail' => array(
       'rule' => array('email'), 
       'message' => 'Please enter valid email', 
       'last' => true), 
      'mustUnique' => array(
       'rule' => 'isEmailUnique', 
       'message' => 'This email is already registered', 
      ) 
     ), 
     'oldpassword' => array(
      'mustNotEmpty' => array(
       'rule' => 'notEmpty', 
       'message' => 'Please enter old password', 
       'last' => true), 
      'mustMatch' => array(
       'rule' => array('verifyOldPass'), 
       'message' => 'Please enter correct old password'), 
     ), 
     'password' => array(
      'mustNotEmpty' => array(
       'rule' => 'notEmpty', 
       'message' => 'Please enter password', 
       'on' => 'create', 
       'last' => true), 
      'complex' => array(
       'rule' => array('complexPassword'), 
       'message' => 'Password should be six characters long, which should have minimum one alphabet, one number and one special character', 
      ), 
     ), 
     'captcha' => array(
      'mustMatch' => array(
       'rule' => array('recaptchaValidate'), 
       'message' => ''), 
     ) 
    ); 
    $this->validate = $validate1; 
    return $this->validates(); 
} 

验证它给我错误

Error: 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 'AddEditValidate' at line 1

SQL Query: AddEditValidate

请帮帮我。

感谢

+0

我知道这个问题问了很多次,但它不会解决我的问题。在用户控制器中,函数是if($ this-> User-> AddEditValidate()){}即使我尝试if($ this-> User-> AddEditValidatee()){}但它给了我相同的错误。在这里我输入AddEditValidatee的问题是错误的。我知道它应该是AddEditValidate。 – Kamal 2014-09-05 09:16:01

+0

检查拼写使用AddEditValidate而不是AddEditValidatee – Abhishek 2014-09-05 09:22:32

+0

使用$ this-> request-> data not $ this-> data,编辑你的问题以便更好的理解,这样别人可以帮助 – Abhishek 2014-09-05 10:07:21

回答

0

数据库科拉姆名在3.0版本中没有报价。您在字段名称中使用了SQL关键字。如果你不能重命名你的列,你应该启用标识符引用。

book.cakephp.org

$conn->driver()->autoQuoting(true); 

By default CakePHP does not quote identifiers in generated SQL queries. The reason for this is identifier quoting has a few drawbacks:

  • Performance overhead - Quoting identifiers is much slower and complex than not doing it.
  • Not necessary in most cases - In non-legacy databases that follow CakePHP’s conventions there is no reason to quote identifiers.

If you are using a legacy schema that requires identifier quoting you can enable it using the quoteIdentifiers setting in your Configuration. You can also enable this feature at runtime:

When enabled, identifier quoting will cause additional query traversal that converts all identifiers into IdentifierExpression objects.

+0

不要链接到外部网站,而不必解释答案中的内容。如果链接中断,您的评论将毫无价值。 – Alan 2018-02-05 04:37:12

+0

虽然这说明了问题可能是什么,但您还没有提供实际的解决方案。这更适合作为评论,你可以用额外的声誉来做。如果可以,请编辑您的回复以提供明确的自给自足的解决方案。如果你不能,那么SO很可能会将其删除为“未答复”。 – SherylHohman 2018-02-05 04:50:27

相关问题