Welcome Guest, Not a member yet? Register   Sign In
CRUD issues
#1

[eluser]Jakebert[/eluser]
Here's my problem, I hope someone can help me see the error!

When I go to edit a student, the information comes up in the form fine. When I try to submit the form I get three PHP Notices:

A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Students::edit()
Filename: admin/students.php
Line Number: 72

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: sid
Filename: admin/students.php
Line Number: 94

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: sid
Filename: admin/students.php
Line Number: 97

Those are all in the controller. Here is the relevant code (sorry that there's so much :-[)

This is the index page (controller):
Code:
function manage()
    {
        $data = $this->students->students(); // List the students
        $this->table->set_heading('ID', 'First', 'Last', 'Age', 'Gender', 'Level'); //Setting headings for the table
        
        foreach($data as $value => $key)
        {
            //Build action links
            $actions = anchor("admin/students/edit/".$key['sid']."/", "Edit") . anchor("admin/students/delete/".$key['sid']."/", "Delete");
            //Adding row to table
            $this->table->add_row($key['sid'], $key['fname'], $key['lname'], $key['age'], $key['gender'], $key['level'], $actions);
        }
        
        $this->auth->view('students/manage'); // Load the view
    }

Here's the edit controller (in students):
Code:
function edit($sid)
    {    
        $this->form_validation->set_rules('fname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('lname', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('age', 'Age', 'trim|required|numeric');
        $this->form_validation->set_rules('gender', 'Gender', 'required');
        $this->form_validation->set_rules('level', 'Level', 'trim|required');
        
        if($this->form_validation->run() == FALSE)
        {
            $data = $this->students->fetch($sid);

            $this->auth->view('students/edit', $data[0]);
        }
        else
        {
            $data['fname'] = set_value('fname');
            $data['lname'] = set_value('lname');
            $data['age'] = set_value('age');
            $data['gender'] = set_value('gender');
            $data['level'] = set_value('level');

            if ($this->students->edit($sid, $data)==1)
            {$this->auth->view('students/edit_success');}
            
            if($this->students->edit($sid,$data)==0)
            {echo 'There was an error with the update process. Sorry!';}

and the data model:
Code:
function fetch($sid)
    {
        $query = $this->db->get_where('students', array('sid' => $sid));
        return $query->result_array();
    }

    function edit($sid, $data)
        {
            $this->db->where('sid', $sid);
            $this->db->update('students', $data);
            return $this->db->affected_rows();
        }

and FINALLY the edit view:
Code:
<fieldset>
<legend>Personal Information</legend>
&lt;?php
echo form_open('admin/students/edit');
?&gt;
First Name: &lt;?php echo form_input('fname', set_value('fname', $fname)); ?&gt;
&nbsp;&nbsp;&nbsp;&nbsp;Last Name: &lt;?php echo form_input('lname', set_value('lname', $lname)); ?&gt;
&lt;?php echo form_error('fname');
echo form_error('lname'); ?&gt;
<br /><br />Age: &lt;?php echo form_input('age', set_value('age', $age)); ?&gt;
&lt;?php echo form_error('age'); ?&gt;
<br />

<br />
Gender:
&lt;?php echo form_radio('gender', set_value('male', 'Male')); ?&gt; Male
&lt;?php echo form_radio('gender', set_value('female', 'Female')); ?&gt; Female

&lt;?php echo form_error('gender'); ?&gt;
</fieldset>

<fieldset>
<legend>Swimming Information</legend>
Level: &lt;?php echo form_input('level', set_value('level', $level));
echo form_error('level'); ?&gt;
</fieldset>

&lt;?php echo form_submit('submit', 'Edit Student');
echo form_close(); ?&gt;

I've tried sending the $sid through the view as a hidden value, that didn't work. I have no idea what the problem could be. Any help would be greatly appreciated!
#2

[eluser]Jan_1[/eluser]
I'm no professional, so prove if it is right what i assume:

controller-function edit() is not getting a $id from the form.
post it to the function
or change function name 'function edit($id)' to 'function edit()' and take another way to get $id.
#3

[eluser]tardyace[/eluser]
you could try in the view:

Code:
form_open('admin/students/edit/'.$sid);

then in the controller

Code:
function edit(){
$sid = $this->uri->segment(4, 0);
...




Theme © iAndrew 2016 - Forum software by © MyBB