2013-11-26 66 views
-5

如果我直接赋值,意味着它将值插入到数据库中。在codeigniter中使用php将数据插入数据库时​​出现错误

但是,如果使用变量(如$user$pass)通过在members_model类值,它显示错误象下面这样:

Parse error: syntax error, unexpected '$user' (T_VARIABLE), expecting function (T_FUNCTION) in C:\xampp\htdocs\application\models\members_model.php on line 5

型号:

members_model.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

    class Members_model extends CI_Model 
    { 
     $user = 'Vinayak'; 
     $pass = 'ggf'; 

     function get_members() 
     { 
      $query = $this->db->get('users'); 
      return $query->result_array(); 
     } 
     function put_members() 
     { 

      $this->username = $_POST['user']; 
      $this->password = $_POST['pass']; 
      $this->db->insert('users', $this); 
     } 
    } 
    ?> 

注意:请帮我解决这个错误,或者告诉我是否有任何错误r使用PHP中的codeIgniter将数据插入MySQL数据库的方法

回答

0

使用类时,如果要定义变量,则需要使用访问说明符。 像在这种情况下:

class Members_model extends CI_Model 
    { 
     $user = 'Vinayak'; 
     $pass = 'ggf'; 

应该是:

class Members_model extends CI_Model 
    { 
     public/private/protected $user = 'Vinayak'; 
     public/private/protected $pass = 'ggf'; 

,并使用同一CALSS这个变量就可以使用他们喜欢$this->user, $this->pass

+0

谢谢你太多了 – user3034972

0
从接入水平@Sankalp

除了说,这是什么导致你显示的错误,你的插入方法有问题(你已经被缺少的acces说明符阻塞了,但是当你继续时,你也会得到这个错误)

所以,

class Members_model extends CI_Model 
{ 
    private $user = 'Vinayak'; 
    private $pass = 'ggf'; 
    // though, why creating class defaults for user and password?? what's the point? 

然后,你的函数:

function put_members() 
    {  
    $this->username = $_POST['user']; 
    $this->password = $_POST['pass']; 
    $this->db->insert('users', $this); 
    } 
} 

错误:你传递给insert()方法整个对象($this)当你必须通过字段名称改为:

更好的方法可以做到这一点成为:

function put_members() 
{ 

    $this->user = $this->input->post('user'); 
    $this->pass = $this->input->post('pass'); 
    $this->db->insert('users', array(
             'user' => $this->user, 
             'pass' => $this->pass 
            ) 
        ); 
} 

其中'user'和'pass'是'用户'表的列名(根据您的设置进行相应更改)。

重要提示:我们不要你存储明文密码,这是真的不好的做法现在说说,但只要那个记住,你有你的模式运作,你应该专注于这个问题太:使用你的PHP安装提供的最佳解决方案(带有BLOWFISH的crypt()将是相当不错的)或者至少是sha1()

相关问题