Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

If I assign values directly means it's inserting values into DB.

But if I use variables(like $user, $pass) to pass values in members_model class, it's showing error like below:

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

Model:

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);
        }
    }
    ?>

NOTE: Please help me to resolve this error or tell me is there any better way to insert data into MySQL Database using codeIgniter in PHP

share|improve this question
add comment

2 Answers

When using a class, if you are defining variables, you need to use a access specifier with it. Like in this case :

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

should be :

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

and is you use this variable in same calss you can use them like $this->user, $this->pass

share|improve this answer
 
Thank you so much frnd –  user3034972 Nov 26 at 6:10
add comment

Apart from the access level as @Sankalp said, which is what's causing the error you shown, you have a problem in your insert method (you've been blocked by the missing acces specifier, but when you go on, you'll get that error too)

So,

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?

Then, your function:

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

Is wrong: you're passing to the insert() method the whole object ( $this ) when you have to pass the fields names instead:

A better way to do that would be:

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
                                    )
                    );
}

Where 'user' and 'pass' are the column names of your 'users' table (change accordingly to your setup).

Important note: Let's not talk now about you storing the password in cleartext, which is really bad practice, but keep in mind that as soon as you have your model functioning you should focus on that issue too: use the best solution your php installation provides (crypt() with BLOWFISH would be pretty nice) or at the very very least sha1()

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.