Welcome Guest, Not a member yet? Register   Sign In
status enum "active" and "inactive" not updating
#1

[eluser]swgj19[/eluser]
Codeigniter version: 2.1.2

I am working on a blog site that has an admin panel. In the admin panel a user can create a category for the blog with a status of active or inactive. When the category is active, the category shows on the home page. When the category status is inactive, the category is saved to the database, but inactive to the public.
For some reason, when creating a category in the admin panel under categories, when status is chosen as "active" or "inactive", the status is not updating when data from form is sent to database.

Below are partials of the mvc.

View: admin_cat_create
Code:
<h1>&lt;?php echo $title;?&gt;</h1>

&lt;?php
echo form_open('admin/categories/create');
echo "<p><label for='catname'>Name</label><br/>";
$data = array('name'=>'name','id'=>'catname','size'=>25);
echo form_input($data) ."</p>";

echo "<p><label for='short'>Short Description</label><br/>";
$data = array('name'=>'short_desc','id'=>'short','size'=>40);
echo form_input($data) ."</p>";

echo "<p><label for='long'>Long Description</label><br/>";
$data = array('name'=>'long_desc','id'=>'long','rows'=>5, 'cols'=>'40');
echo form_textarea($data) ."</p>";

echo "<p><label for='status'>Status</label><br/>";
$options = array('active' => 'active', 'inactive' => 'inactive');
echo form_dropdown('status',$options) ."</p>";


echo "<p><label for='order'>Sort Order</label><br/>";
$data = array('name'=>'sort_order','id'=>'order','size'=>5);
echo form_input($data) ."</p>";


echo form_submit('submit','create category');
echo form_close();

Controller: categories

Code:
function create(){
    if ($this->input->post('name')){
    $this->MCats->addCategory();
    $this->session->set_flashdata('message','Category created');
    redirect('admin/categories/index','refresh');
   }else{
  $data['title'] = "Create Category";
  $data['main'] = 'admin_cat_create';
  $data['categories'] = $this->MCats->getTopCategories();
  $this->load->vars($data);
  $this->load->view('dashboard');    
}
  }

Model: mcats
Code:
function addCategory(){
$data = array(
  'name' => str_replace(" ", "_",$this->input->post('name')),
  'short_desc' => $this->input->post('short_desc'),
  'long_desc' => $this->input->post('long_desc'),
                //status in form holds array
                //'status' => $this->input->post('status',$options),
  'status' => $this->input->post('status'),
  'sort_order' => $this->input->post('sort_order')
        
);

$this->db->insert('categories', $data);

}

so looking at the above code, $options is passed with the 'status' name in the view. And in the database I have a table called categories with a column named 'status' with the type set as enum 'active' and 'inactive'. 'active' is default.

So far, the form when submitted is not updating the database.
I think the problem has to do with passing $options.

Any pointers on where the problem lies? Thanks for your input.
#2

[eluser]Aken[/eluser]
Well first, look at your HTML output. Does it look correct?

Second, does your enum column in the DB actually have the name "status" ?

The code looks okay at first glance, so it's probably something silly that you're overlooking.
#3

[eluser]swgj19[/eluser]
As far as I see, the view looks fine.

Quote:Well first, look at your HTML output. Does it look correct?
Code:
<h1>Create Category</h1>

&lt;form action="http://localhost/blog_wrox/index.php/admin/categories/create" method="post" accept-charset="utf-8"&gt;&lt;p>
<label for='catname'>Name</label><br/>&lt;input type="text" name="name" value="" id="catname" size="25"  /&gt;&lt;/p><p>
<label for='short'>Short Description</label><br/>&lt;input type="text" name="short_desc" value="" id="short" size="40"  /&gt;&lt;/p><p>
<label for='long'>Long Description</label><br/>&lt;textarea name="long_desc" cols="40" rows="5" id="long" &gt;&lt;/textarea></p><p>
<label for='status'>Status</label><br/><select name="status">
<option value="active">active</option>
<option value="inactive">inactive</option>
</select></p><p><label for='order'>Sort Order</label><br/>&lt;input type="text" name="sort_order" value="" id="order" size="5"  /&gt;&lt;/p>&lt;input type="submit" name="submit" value="create category"  /&gt;&lt;/form>  </div>

The form looks correct, because the array is passing as option values in the form.

Quote:Second, does your enum column in the DB actually have the name “status” ?

Yes:

Quote:CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
`shortdesc` varchar(255) NOT NULL DEFAULT '',
`longdesc` text NOT NULL,
`status` enum('active','inactive') NOT NULL DEFAULT 'active',
`sortorder` int(3) NOT NULL DEFAULT '0',
`parentid` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

#4

[eluser]Aken[/eluser]
echo $this->db->last_query() after your insert and see what SQL is being executed.

Also how are you checking that the status is set? Through your code, or directly through phpMyAdmin or DB program? Maybe your raw data is fine, but you're querying it wrong or something.
#5

[eluser]swgj19[/eluser]
I added validation in the controller with condition:

Code:
function create($options=''){
   $this->load->helper(array('form', 'url'));

  $this->load->library('form_validation');

  $this->form_validation->set_rules('name', 'Name', 'required');
  $this->form_validation->set_rules('short_desc', 'Short Description', 'required');
  $this->form_validation->set_rules('long_desc', 'Long Description', 'required');
  $this->form_validation->set_rules('status', 'Status', 'required');
  $this->form_validation->set_rules('sort_order', 'Sort Order');

  if ($this->form_validation->run() == FALSE)
  {
   $data['title'] = "Create Category";
   $data['main'] = 'admin_cat_create';
   $data['categories'] = $this->MCats->getTopCategories();
   $this->load->vars($data);
   $this->load->view('dashboard');  
  }else{
   $this->MCats->addCategory();
     $this->session->set_flashdata('message','Category created');
     redirect('admin/categories/index','refresh');
  }

  }

I also checked the the query in the mcat controller and everything looks correct. The database will still not update the status. Thanks for your suggestions.
#6

[eluser]Aken[/eluser]
I don't know what else to tell you without seeing actual data. Maybe it's a character set issue or something, I can't tell based on this.

You keep saying "will not update the status". You're not updating anything, you're inserting new rows with this code.
#7

[eluser]swgj19[/eluser]
What I meant to say is that in the categories table, every column is being inserted from the form in the view to the db except for the input column 'status'. I really do appreciate your time. I will keep debugging and see whats going on.
#8

[eluser]swgj19[/eluser]
Ok, for some reason chrome cache was not clearing. I switched browsers to mozilla and the categories table and status column is changing fine. However, on the view or home page of the blog, the categories are not going away when the status is changed to 'inactive'. I am half way there.
#9

[eluser]Aken[/eluser]
That's why I said check the actual data source, rather than what you pull from your browser. The other problem is your code.
#10

[eluser]swgj19[/eluser]
Quote:The other problem is your code.

Could you be specific?




Theme © iAndrew 2016 - Forum software by © MyBB