CodeIgniter Forums
form field not updating - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: form field not updating (/thread-57639.html)

Pages: 1 2


form field not updating - El Forum - 03-29-2013

[eluser]deongee[/eluser]
I have a edit form that updates all fields accept 1. The account_id field, which is not the primary id field will not update. Iā€™m testing the ā€œaccount_idā€ input field. Here is my code.

Controller: manage.php
Code:
function edit($account_id='', $platform =''){
  if($account_id > ''){
$query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
if(isset($query))
if ($query->num_rows() > '')
{
     $row = $query->row_array();
}
  $data['account_id']    = '';
  $data['platform']    = '';
  $data['name']     = '';
  $data['location_city']   = '';
  $data['location_country'] = '';
  $data['status']    = '';
  $data['region']    = '';
  $data['link']    = '';
  $data['type']    = '';
  
  $data['faccount_id']['value']  = $row['account_id'];
  $data['fplatform']['value']  = $row['platform'];
  $data['fname']['value']  = $row['name'];
  $data['flocation_city']['value']= $row['location_city'];
  $data['flocation_country']['value'] = $row['location_country'];
  $data['fstatus']['value']  = $row['status'];
  $data['fregion']['value']  = $row['region'];
  $data['flink']['value']  = $row['link'];
  $data['ftype']['value']  = $row['type'];
}
if(isset($account_id)){
  $this->form_validation->set_rules('account_id', 'Account ID', 'trim|required');
  $this->form_validation->set_rules('platform', 'Platform', 'trim|required');
  $this->form_validation->set_rules('name', 'Name', 'trim|required');
  $this->form_validation->set_rules('status', 'Status', 'trim|required');
  $this->form_validation->set_rules('location_city', 'City', 'required');
  $this->form_validation->set_rules('location_country', 'Country', 'required');
  $this->form_validation->set_rules('region', 'Region', 'required');
  $this->form_validation->set_rules('link', 'Link', 'trim|required');
  $this->form_validation->set_rules('type', 'Type', 'trim|required');
  if($this->form_validation->run()){
  if($query = $this->dashboard_model->update_account()){ //If successful update
  $data['message'] = ' - Update complete!';
      }else{
   $data['message'] = 'There was an error, update not complete.';
   }}  
$data['account'] = $this->dashboard_model->get_account($account_id,$platform);
$this->load->view('manage/edit',$data);
    }
}

Model: dashboard_model.php
Code:
function update_account(){
$data = array(
'account_id'=>$this->input->post('account_id'),
'name'=>$this->input->post('name'),
'status' => $this->input->post('status'),
'location_city' => $this->input->post('location_city'),
'location_country' => $this->input->post('location_country'),
'region' => $this->input->post('region'),
'link' => $this->input->post('link'),
'type' => $this->input->post('type'));
$where = array(
      'account_id' =>  $this->input->post('account_id'),
      'platform' => $this->input->post('platform'));
$update = $this->db->update('db_accounts', $data, $where);
    return $update;}
}?>

View: edit.php
Code:
<?php
$hidden = array(
'account_id'  => $faccount_id['value'],
'platform'  => $fplatform['value'],
'name'   => $fname['value'],
'status'  => $fstatus['value'],
'location_city' => $flocation_city['value'],
'location_country' => $flocation_country['value'],
'region'  => $fregion['value'],
'link'   => $flink['value'],
'type'   => $ftype['value']);
echo '<form method="post" accept-charset="utf-8">';
echo form_hidden($hidden);
echo '<span font-weight:bold"><font color="#C40909">'.$fplatform['value'].'</font>' ;
if(isset($message))
{echo '<font color=#C40909> '.$message.'</font></span>';};
echo '<h1 class="header"><span class="title">' .set_value('name', $fname['value']).'</span></h1>'; ?&gt;
<table width="840" cellpadding="0" cellspacing="0" cols="3"  color:#000; font-weight:bold; margin-top:10px;">
<tr><td>&lt;?php echo form_label('Name', 'name');
echo '<span  color:#C40909;">'.form_error('name',' *');
echo form_input('name', set_value('name', $fname['value']),'style="width: 388px;"'); '</span>'?&gt;</td>
<td>&lt;?php echo form_label('Status', 'status');
$status_options = $this->lang->line('status');
echo form_dropdown('status', $status_options, set_value('status', $fstatus['value']), 'style="width: 200px; height:30px;"');?&gt;</td>
<td>&lt;?php echo form_label('Type', 'type');
$type_options = $this->lang->line('type');
echo form_dropdown('type', $type_options, set_value('type', $ftype['value']), 'style="width: 200px; height:30px;"');?&gt;</td>
</tr>
<tr><td>&lt;?php echo form_label('City', 'location_city');echo '<span  color:#C40909;">'.form_error('location_city',' *'); echo form_input('location_city', set_value('location_city', $flocation_city['value']),' 388px;"'); '</span>' ?&gt;</td>
<td colspan="2">&lt;?php echo form_label('Region', 'region');
$region_options = $this->lang->line('region');
echo form_dropdown('region', $region_options, set_value('region', $fregion['value']), 'style="width: 410px; height:30px;"');?&gt;
<span  color:#C40909;">&lt;?php echo form_error('region',' '); ?&gt;</span></td></tr>
<tr><td  height="20px;">Country <span  color:#C40909;">&lt;?php echo form_error('location_country',' *');?&gt;</span>
&lt;?php echo form_input('location_country', set_value('location_country', $flocation_country['value']),'style="width: 388px;"'); ?&gt;</td>
<td colspan="2">&lt;?php echo form_label('Link', 'link');
echo '<span  color:#C40909;">'.form_error('link',' *'); echo form_input('link', set_value('link', $flink['value']),' 388px;"'); '</span>' ?&gt;</td></tr>
<tr><td colspan="2">&lt;?php echo form_label('Account ID', 'account_id');
echo '<span  color:#C40909;">'.form_error('account_id',' *');  
echo form_input('account_id', set_value('account_id', $faccount_id['value']),'style="width: 388px;"'); '</span>'?&gt;</td></tr>
<tr><td colspan="3">&lt;?php $data = array('name' => 'submit', 'value' => 'Update Account');echo form_submit($data); echo form_close();?&gt;</td></tr></table></div></div>



form field not updating - El Forum - 03-29-2013

[eluser]TheFuzzy0ne[/eluser]
Are any of your calls to set_value() actually working as expected. I see quite a few potential problems.

I'm struggling to follow the flow of your code because you're missing an opening curly braces here:
Code:
function edit($account_id='', $platform =''){
    if($account_id > ''){
        $query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
        if(isset($query)) // here
        if ($query->num_rows() > '')
        {

Here are some suggestions (that hopefully point out some other logic errors):
Code:
function edit($account_id='', $platform =''){
    // if($account_id > '') {
    if ($account_id) {
        $query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
        
        // if(isset($query)) { // $query is already set to the return value of the function call above.
        if ($query) {
            // if ($query->num_rows() > '') // num_rows() returns an integer.
            if ($query->num_rows() > 0)
            {
                $row = $query->row_array();
            }
            
/*      // Or maybe:
        if ($query AND $query->num_rows() != 0) {
            $row = $query->row_array();
*/            
            $data['account_id']    = '';
            $data['platform']    = '';
            $data['name']     = '';
            $data['location_city']   = '';
            $data['location_country'] = '';
            $data['status']    = '';
            $data['region']    = '';
            $data['link']    = '';
            $data['type']    = '';

            $data['faccount_id']['value']  = $row['account_id'];
            $data['fplatform']['value']  = $row['platform'];
            $data['fname']['value']  = $row['name'];
            $data['flocation_city']['value']= $row['location_city'];
            $data['flocation_country']['value'] = $row['location_country'];
            $data['fstatus']['value']  = $row['status'];
            $data['fregion']['value']  = $row['region'];
            $data['flink']['value']  = $row['link'];
            $data['ftype']['value']  = $row['type'];
        }
        // if (isset($account_id)) { // $account_id is already set to whatever was passed into the controller method, or the default value.
        // If we've got this far, then we must have a valid account_id, so no point checking for it anyway.
            $this->form_validation->set_rules('account_id', 'Account ID', 'trim|required');
            $this->form_validation->set_rules('platform', 'Platform', 'trim|required');
            $this->form_validation->set_rules('name', 'Name', 'trim|required');
            $this->form_validation->set_rules('status', 'Status', 'trim|required');
            $this->form_validation->set_rules('location_city', 'City', 'required');
            $this->form_validation->set_rules('location_country', 'Country', 'required');
            $this->form_validation->set_rules('region', 'Region', 'required');
            $this->form_validation->set_rules('link', 'Link', 'trim|required');
            $this->form_validation->set_rules('type', 'Type', 'trim|required');
            if($this->form_validation->run()) {
                if ($query = $this->dashboard_model->update_account()) { //If successful update
                    $data['message'] = '&nbsp;- Update complete!';
                } else {
                    $data['message'] = 'There was an error, update not complete.';
                }
            }
        
        // Can't you set this at the top of the method? Where are you checking the validity of $platform?
        // I would recommend setting a default for it.
        $data['account'] = $this->dashboard_model->get_account($account_id, $platform);
        
        // Shouldn't this be loaded at all times, not just when the edit is successful?
        $this->load->view('manage/edit',$data);
    }
    
    // What happens when there's no account ID specified, or it doesn't exist?
}



form field not updating - El Forum - 03-29-2013

[eluser]deongee[/eluser]
The account_id is passed from another page.

Code:
<span ><a href="&lt;?php echo $config['dash_base_url'].'manage/edit/'.$account-&gt;account_id ?&gt;"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</a></span>
</div>

When the page loads it passes the account_id variable url to edit.php view page.


This is the only field that is not updating, All the other fields are updating okay.

Code:
&lt;?php
echo form_label('Account ID', 'account_id');
echo '<span  color:#C40909;">'.form_error('account_id',' *');  
echo form_input('account_id', set_value('account_id', $faccount_id['value']),'style="width: 388px;"'); '</span>'
?&gt;




form field not updating - El Forum - 03-29-2013

[eluser]TheFuzzy0ne[/eluser]
Sorry. I see no reason why that shouldn't work if the others are working. Just to be clear: Have you tried changing the values of the other fields to something other than the default? Does that definitely work as expected when you submit the form? Can you get errors to display on your form?

Have you enabled error reporting? Place this at the top of your index.php file:
Code:
ini_set('display_errors', '1');
error_reporting(E_ALL);

That might show something. Even if it doesn't, it's good to keep it enabled while you're developing your app.


form field not updating - El Forum - 03-29-2013

[eluser]deongee[/eluser]
Yes i have this in place and I'm not getting an error. when I update this field, it updates immediately, but when I go back to the account page where the account_id is being passed, then back to the edit.php page, this field goes back to the original data.

Maybe its because I'm passing this as a variable string to the next page and since it is getting cached it will not update.

For exampe, if the account_id value is 123456Test, the url that is getting passed is http://mywebsite/manage/edit/123456Test/.

Since 123456Test (account_id) is part of the url maybe this is why I can't update it? What do you think?


form field not updating - El Forum - 03-29-2013

[eluser]TheFuzzy0ne[/eluser]
I'm not quite sure I follow you. All the edit pages in my app take an ID from the URL, and I haven't had any problems. However, I do redirect to another page after, to prevent resubmission of the form if the back button is clicked. Are you clicking the back button, or are you clicking on a link to get from page to page?


form field not updating - El Forum - 03-29-2013

[eluser]deongee[/eluser]
I don't use the back button. I created a link on the edit page to navigate back to the previous page. Here is this little bit of code. this way it returns to the account to show all updates.

Code:
<a href="/platform/&lt;?php echo $fplatform['value'].'/account/'.$faccount_id['value']; ?&gt;"> <span  color:#C40909;">Return to: </span>&lt;?php echo set_value('name', $fname['value'])?&gt;</a>

By the way not to confuse, this is not the primary key record (id), this is just a field called account_id to identify youtube, facebook, twitter accounts.

table structure
id
account_id
platform
status
name
region
location_city
location_country
link



form field not updating - El Forum - 03-29-2013

[eluser]TheFuzzy0ne[/eluser]
Let's say the someone submitted an invalid account ID or platform. What would you expect your app to do then? I'm asking, because I still can't make out your application flow. From what I see, if you leave $account_id empty, the page won't even display. Is that right?


form field not updating - El Forum - 03-29-2013

[eluser]deongee[/eluser]
The account_id will never be blank because it is pass from the main page.
The edit page is for accounts that are already in the database. This is a huge application and I can go on and on with it based on the questions you are asking. I will end up posting the whole application which works fine. The only issue I'm having is with one field (account_id) not getting updated.

There is a page that creates accounts. The user can only enter the account id (facebook, twitter and so on) and platform which is a controlled drop down list. An error is passed if the account exists or invalid, which has nothing to do with the edit page.

I will figure it out. Thanks for your help.



form field not updating - El Forum - 03-29-2013

[eluser]TheFuzzy0ne[/eluser]
That's right, but some people will manipulate the URL to intentionally break your app, or find weaknesses and/or exploits. The reason I asked is because I was going to have a bash at restructuring your method.

As I mentioned before, I see no immediate reason why what you've got shouldn't work, based on the fact that the rest seems to work. What could be responsible for this? The weather? Fluctuations in the stock market? I have a hunch that it's the code that's the problem, since you're doing a lot of checks that serve no real purpose. If you structure your code a bit better, it will be more robust, and a heck of a lot easier to debug.

This code is untested, but it's cleaner, more robust, easier to follow and debug, less code than before and there's not a nested if-statement in sight.
Code:
function edit($account_id = '', $platform = '')
{
    // Check that the account exists. You can pull the default form field values for your views from here.
    if ( ! $data['account'] = $this->dashboard_model->get_account($account_id, $platform)) { // This is where you should validate the account_id and platform.
        show_error('Sorry, but we were unable to locate the requested account!');
    }
    
    // If we've got this far, we have an account.
    $data['account_id']    = '';
    $data['platform']    = '';
    $data['name']     = '';
    $data['location_city']   = '';
    $data['location_country'] = '';
    $data['status']    = '';
    $data['region']    = '';
    $data['link']    = '';
    $data['type']    = '';

    // Set validation rules.
    $this->form_validation->set_rules('account_id', 'Account ID', 'trim|required');
    $this->form_validation->set_rules('platform', 'Platform', 'trim|required');
    $this->form_validation->set_rules('name', 'Name', 'trim|required');
    $this->form_validation->set_rules('status', 'Status', 'trim|required');
    $this->form_validation->set_rules('location_city', 'City', 'required');
    $this->form_validation->set_rules('location_country', 'Country', 'required');
    $this->form_validation->set_rules('region', 'Region', 'required');
    $this->form_validation->set_rules('link', 'Link', 'trim|required');
    $this->form_validation->set_rules('type', 'Type', 'trim|required');
    
    // Make sure validation passes, and
    if($this->form_validation->run() AND $this->dashboard_model->update_account()) {
        // Here, I'd normally set a flash message redirect back to the view method.
        $data['message'] = '&nbsp;- Update complete!';
    }
    
    // Load the view.
    $this->load->view('manage/edit', $data);
}

I'd be interested to know if it makes any difference. It might not but it should be much easier to debug.

show_error() isn't necessarily the best way to handle this, but it's better than showing a blank page, no?

It's entirely up to you whether or not you accept my help. I leave it to you.