• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with my CRUD function

#1
[eluser]minoh[/eluser]
hello guys, for my first CI app i want to use a CRUD function to administrate different tables.

i success using an example to create the CR function, but i have problem retrieving 'id' in the view/update/delete functions.

the CI errors :
Code:
A PHP Error was encountered
Severity: Warning

Message: Missing argument 1 for Person::view()

Filename: admin/person.php

Line Number: 139

------------------------------------------------

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: id

Filename: admin/person.php

Line Number: 145

and this is my view() function :

Code:
function view($id)
{
        // set common properties
        $data['title'] = 'Details sur l\'utilisateur';
        $data['body'] = '/admin/personView';
        $data['link_back'] = anchor('admin/person/index/','Retour à la liste d\'utilisateurs',array('class'=>'back'));
            
        // get person details
        $data['person'] = $this->mUtilisateurs->get_by_id($id)->row();
            
        // load view
        $this->load->view('vDashboard', $data);
}

thanks.

#2
[eluser]xzela[/eluser]
dude,

the above error is simply stating that it can not find the first parameter ($id). This means you are not properly supplying the correct parameters when calling this function. Recheck the line of code which calls the 'view' function. You should find the error at that location.

#3
[eluser]BrianDHall[/eluser]
In your code:

Code:
function view($id)

This says "this function MUST be called with a parameter, or error".

While this is logically ok, the problem is you are assuming in your function that $id was set somewhere - but it might not have been.

You can set a default value for $id, here:

Code:
function view($id = 1)

To pass a variable to the function the url should be something like website.com/person/view/1 - but if someone rips off the 1 they'll get that ugly error about $id being undefined, so you'll want to make sure its set before you refer to it.

#4
[eluser]minoh[/eluser]
yes i can get the $id in the url but i still have de same errors.

the problem can be from CI url helper ? because i create a new site where i only use the CRUD function and it's works.

#5
[eluser]kurucu[/eluser]
What URL are you entering? I assume you know it should be /yoursite/view/1 and not /yoursite/view?id=1. Come to think of it, if you are not using index.php, is your .htaccess file configured correctly?

Quick test: do other controller functions that take parameters work OK?

#6
[eluser]minoh[/eluser]
- the URL is good 'MySite/admin/person/view/1".
- i'm using this .htacess to take off the index.php :
Code:
RewriteEngine On
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*) index.php?/&1 [L,QSA]

- it doesnt work with a simple controler to take the parameter, also by trying to delete a record with a simple
Code:
$this->db->where('id', $this->uri->segment(3));
$this->db->delete($this->tbl);

#7
[eluser]JoostV[/eluser]
Counting the params in your URI, your ID appears to be in segment 4, not 3. So
Code:
$this->db->where('id', $this->uri->segment(4));

If you are not sure which segment your param is in, test you params using
Code:
var_dump($this->uri->segment_array());

#8
[eluser]minoh[/eluser]
[quote author="JoostV" date="1253052067"]Counting the params in your URI, your ID appears to be in segment 4, not 3. So
Code:
$this->db->where('id', $this->uri->segment(4));

If you are not sure which segment your param is in, test you params using
Code:
var_dump($this->uri->segment_array());
[/quote]
the var_dump returns : array(0){} as result.

normally it should returns array(4){...}.

#9
[eluser]JoostV[/eluser]
There's your problem, then. Now all you have to do is figure out why...

#10
[eluser]minoh[/eluser]
i dont have any idea, i'm starting with ci. as i post before by testing the function in a new ci site, the function works, but when i copy it in my project doesnt work. the problem could come from url helper ?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.