Welcome Guest, Not a member yet? Register   Sign In
Why does this not work?
#1

[eluser]cz231[/eluser]
Hi,

So I'm building an application with this awesome PHP framework called codeigniter ( ;-P ), and the application has several widgets. One widget is a to-do list and another one is a user list. I'm trying to build the "update" component to both widgets now. I've successfully build the to-do one, and it works. So I recycled the code over to the user list and replaced all of the info that needed to change for this widget. But it doesn't work for some reason. I've spent days on what seems like a really silly issue that's solution is staring me right in the face. So maybe is you guys look at it, you could spot it.

Thanks and here's the code:

Here is my update class in my members controller:

function update() {
$this->form_validation->set_rules('Username', 'User Name', 'required|max_length[255]|htmlspecialchars');
$this->form_validation->set_rules('Password', 'Password', 'required|max_length[255]|htmlspecialchars');
$this->form_validation->set_rules('Email', 'Email', 'valid_email|required|htmlspecialchars');
$this->form_validation->set_rules('Name', 'Name', 'required|max_length[255]|htmlspecialchars');
$this->form_validation->set_rules('Title', 'Title', 'max_length[255]|htmlspecialchars');
$this->form_validation->set_rules('AddressLine1', 'Address Line 1', 'htmlspecialchars');
$this->form_validation->set_rules('AddressLine2', 'Address Line 2', 'htmlspecialchars');
$this->form_validation->set_rules('HomePhone', 'Home Phone', 'htmlspecialchars');
$this->form_validation->set_rules('CellPhone', 'Cell Phone', 'htmlspecialchars');
$this->form_validation->set_rules('BusinessPhone', 'Business Phone', 'htmlspecialchars');
$this->form_validation->set_rules('Group', 'Group', 'htmlspecialchars');

if ($this->form_validation->run() == FALSE) {
$data['title'] = "Editing Member";
$this->load->model('members_model');
$data['members'] = $this->members_model->getID();
$this->load->view('header', $data);
$this->load->view('widgets/members_edit', $data);
$this->load->view('footer', $data);
}
else {
$this->load->model('members_model');
$this->members_model->update();
$this->session->set_flashdata('success', 'Member information updated successfully');
redirect('members/');
}

}

And here's my update function:


function update() {
$id = $this->uri->segment(3);
$data = array(
'Username' => $this->form_validation->set_value('Username'),
'Password' => $this->form_validation->set_value('Password'),
'Email' => $this->form_validation->set_value('Email'),
'Name' => $this->form_validation->set_value('Name'),
'Title' => $this->form_validation->set_value('Title'),
'AddressLine1' => $this->form_validation->set_value('AddressLine1'),
'AddressLine2' => $this->form_validation->set_value('AddressLine2'),
'HomePhone' => $this->form_validation->set_value('HomePhone'),
'CellPhone' => $this->form_validation->set_value('CellPhone'),
'BusinessPhone' => $this->form_validation->set_value('BusinessPhone'),
'Group' => $this->form_validation->set_value('Group')
);

$this->db->where('UserID', $id);
$this->db->update('users2', $data);
}

Do you see anything wrong with this? I've debugged it so I know that it is successfully loading the model and the model class, but I can't see anything wrong with it.

I'd appreciate any help you guys could give. Thanks
#2

[eluser]megabyte[/eluser]
Whats not working?

A few this I do when debugging is output the queries using $this->db->last_query(), that way you can tell if they are being executed.

Another great trick, is to output a random stirng such as "echo "test"; going line by line in your controller. This will tell you if your methods are executing the way you expect them to.

You can also use these 2 debugging tricks in your models and libraries.


And when writing models and library methods, I always have a return value of true if its succesful, or FALSE, NULL if it isnt.
#3

[eluser]cz231[/eluser]
Sorry...everything is working, except the database is not updating. Although it still redirects to the page like it did.
#4

[eluser]megabyte[/eluser]
Then you need to disable redirects, and output your queries.
#5

[eluser]cz231[/eluser]
Thanks! Your help led me to find the problem.

For some reason this line in the model:

$id = $this->uri->segment(3);

is assigning the ID to 0 instead of the actual ID number. I'm unsure as to why this is happening. It is in the same spot in the model as the other example, but it isn't working here. Do you know why? Am I doing something wrong?
#6

[eluser]Jondolar[/eluser]
Are you sure that when your form posts that the action property is set to your full URI?
<form action='/controller/action/3'>

Check your form by looking at the html code and ensure that it is set.
#7

[eluser]cz231[/eluser]
Thanks for the quick reply! That was the issue. Thankyou both of you for all of your help. Issue resolved.
#8

[eluser]megabyte[/eluser]
Echo out all the url segments.

If its zero, chances are the segment you thinkyour referencing is not the right one.

Also, look at the url your form is pointing to. When you submit the form, its going to take the id form the url segment the form is referencing, not the cureeent page the form is on.

so if your script is at www.example.com/admin/edit/3


and your form tag is pointed at

www.example,com/admin/save


the url segment wouldnt exist.
#9

[eluser]megabyte[/eluser]
lol. he beat me to it. glad we both helped.
#10

[eluser]cz231[/eluser]
Haha...thanks megabyte.




Theme © iAndrew 2016 - Forum software by © MyBB