Issue passing $id into Controller

I think I have gotten myself confused over what I am trying to code:

In my view I have a option select field with the

value=""

set as id and the selected text as the description. When this is selected it is outputted using JS into the input box below. On submit I am trying to pass the id of the caption to be updated back into the database but it does not seem to be updating.

What would the best way to pass my id?

On further investigation I have discovered that my if update statement is being passed the $caption but not the id is this due to it being set as a select value?

It also has come to my attention that echo

echo $this->db->last_query();

into the if statement it is coming up with

 SELECT * FROM (`images`)

. Why is this?

View:


        <?php
        //Setting form attributes
    $formImageCaption = array('id' => 'imageCaption', 'name' => 'imageCaption');
    $formCaptionInput = array('id' =>'captionInput', 'name' => 'captionInput', 'style' => 'width:70%', 'class' => 'validate[required[custom[onlyLetterSp]]]');
    $formSubmit = array('id' => 'submit','name' => 'submit','value' => 'Edit Caption');
    ?>
    <div id ="formLayout">
    <?php echo form_open('admin/imagecaption',$formImageCaption);
     	  echo form_fieldset(); 
    ?>
    <p>Please Select A Caption Below To Edit: </p>
    	
    	<section id = "validation"><?php echo validation_errors();?></section>
    	<?php
    	if($success == TRUE) {
    	echo '<section id = "validation">Caption Updated</section>';	
    	}
    	?>
    	<?php echo form_label ('Caption:', 'caption');?>
    
    <select name="captionSelect" id="captionSelect">
    	<?php foreach ($get_images as $image){
    		echo '<option value="'.$image['id'].'">'.$image['description'].'</option>';
    	};
    	?>
    	
    </select>
    	<?php echo form_input ($formCaptionInput); ?>
    	<div id="errorCaptionInput"><?php echo form_error($formCaptionInput);?></div>
    <?php echo form_submit ($formSubmit);?>	
    <?php echo form_fieldset_close();
    	  echo form_close(); ?>
    </div>

Controller:


      function index($id){
    
            if(!$this->session->userdata('logged_in'))redirect('admin/home');
    
    			$data['title'] = 'Image Captions';
    			$data['cms_pages'] = $this->navigation_model->getCMSPages();
    			$data['sales_pages'] = $this->sales_model->getSalesPages();
    			$data['get_images'] = $this->image_model->getImages();
    	        $data['content'] = $this->load->view('admin/imagecaption', $data, TRUE); #Loads the "content"
    	        $this->load->view('admintemplate', $data); #Loads the given template and passes the $data['content'] into it
    
    		if ($this->input->post('submit')){
    			
    					#The User has submitted updates, lets begin!
    
    					#Set The validation Rules	
    					$this->form_validation->set_rules('captionInput', 'Caption', 'trim|required|xss_clean');
    
    						if ($this->form_validation->run() == FALSE){ #Form Validation Fails Load The Default Page
    
    						$data['title'] = 'Image Captions';
    						$data['cms_pages'] = $this->navigation_model->getCMSPages();
    						$data['sales_pages'] = $this->sales_model->getSalesPages();
    						$data['get_images'] = $this->image_model->getImages();
    						$data['content'] = $this->load->view('admin/imagecaption', $data, TRUE); #Loads the "content"
    						$this->load->view('admintemplate', $data); #Loads the given template and passes the $data['content'] into it
    
    					}// END Form Validation 
    					
    					#Form Validation passed, so lets continue updating.
    					#lets set some variables to pass into the database for editing.
    
    						$caption = $this->input->post('captionInput', TRUE);
    						$this->db->escape($caption); # Lets check for security and mel objects :)
    
    					#Now if imageCaption fails to update the database then show "there was a problem".
    
    						if($this->image_model->updateCaption($id, $caption)) {
    							$data['title'] = 'Image Captions';
    							$data['cms_pages'] = $this->navigation_model->getCMSPages();
    							$data['sales_pages'] = $this->sales_model->getSalesPages();
    							$data['get_images'] = $this->image_model->getImages($id);
    							$data['success'] = TRUE;
    							$data['content'] = $this->load->view('admin/imagecaption', $data, TRUE); #Loads the "content"
    							$this->load->view('admintemplate', $data); #Loads the given template and passes the $data['content'] into it	
    
    							}//END if updatePage
    						}else{
    							$data['title'] = 'Image Captions';
    							$data['cms_pages'] = $this->navigation_model->getCMSPages();
    							$data['sales_pages'] = $this->sales_model->getSalesPages();
    							$data['get_images'] = $this->image_model->getImages();
    							$data['content'] = $this->load->view('admin/imagecaption', $data, TRUE); #Loads the "content"
    							$this->load->view('admintemplate', $data); #Loads the given template and passes the $data['content'] into it
    	
    		} //END Submit 
    			
        } //END function index()
    		
    }

Model:


    class Image_model extends CI_Model
    {
    
    	function __construct() {
    			parent::__construct();
    	}
    
    	function getImages($path = NULL) {
    	foreach($this->db->get('images')->result_array() as $r) {
    
    		$rows[] = $r;
    	}
    
    	return $rows;
    	}
    
    	function addImage($imgdata) {
    		$this->db->insert('images',$imgdata);
    		return;
    	}
    
    	function deleteimage($id){
    		
    		$this->db->where('id', $id);
    		$q = $this->db->get('images');
    		$row = $q->row_array();
    		
    		if ($q->num_rows() > 0){
    			//delete from the database
    			$this->db->where('id', $id); 
    			$this->db->delete('images');
    
    			//lets delete the image
    			unlink("includes/uploads/gallery/".$row['imagename']);
    			//lets delete the thumb.
    			unlink("includes/uploads/gallery/thumbs/".$row['thumbname']);
    		}//END if num_rows
    	}//END function deleteImage($id)
    	
    	function updateCaption($id = NULL, $caption = NULL){
    		#set the $data passed to the function into an array, content being the column name.
    		$data = array('description' => $caption);
    		
    		$this ->db->where('id',$id);
    		$this->db->update('images', $data);
    		
    		return TRUE;
    	}
    
    }//END class Image_model

You call updateCaption with a null/wrong $id, because you never assign anything to $id before calling that function. Do a search for “$id =” in your controller, that text never appears. $id may be prepopulated by whatever framework you’re using (a mystery), but it’s definitely not coming from your select box.

Your select is named captionSelect but you never read that from your form. Do a search for “captionSelect” in your controller, that text never appears.

So the problem is that you failed to write any code to use this select box at all.

Thank you,

I can see where you are coming from now, I am using CI. Could someone provide me with an outline or guide how I would pass the id of the selected object to the input box so when I update the specific item it will carry the ID with it into my update function.

Resolved