How to get id from URL in codeigniter?

I am trying to get ‘id’ from URL in order to delete that specific selected product. I have used $this->uri->segment(3) but its not getting any value from URL. Although i can see the ‘product_id’ in my URL.

If i used $this->url->segment(2) it gives me the second value form URL, but i am unable to get product Id.
Following is my code. Kindly guide me.


echo "<a href='delete_controller?product_id=$product_id'>";
echo $name;
echo "</a>";

The following URL is generating when i click on $name.



public function delete_controller()
        echo $product_id =$this->uri->segment(3);           
        echo "Taha";

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Check the below code hope that you get your parameter

echo $this->uri->segment('3');

Solution 2

if you have to pass the value you should enter url like this


and in controller function you can read like this

function delete_controller( $product_id = NULL ) {
  echo $product_id;

Solution 3

$product_id = $this->input->get('id', TRUE);
echo $product_id;

Solution 4

View page

  <a href="<?php echo base_url();?>products_controller/delete_controller/<?php echo $product_id;?>" rel="nofollow noreferrer noopener"><?php echo $name; ?></a>

controller page

  function delete_controller( $product_id) {

         echo $product_id;
         //add your logic


Solution 5

In codeigniter you can’t pass parameters in the url as you are doing in core php.So remove the “?” and “product_id” and simply pass the id.If you want more security you can encrypt the id and pass it.

Solution 6

I think you’re using the CodeIgniter URI routing wrong:

Basically if you had a Products_controller with a method called delete($id) and the url you created was of the form http://localhost/designs2/index.php/products_controller/delete/4, the delete function would receive the $id as a parameter.

Using what you have there I think you can get the id by using $this->input->get('product_id);

Solution 7

A bit late but this worked for me

  $data_id = $this->input->get('name_of_field');

Solution 8

$CI =& get_instance();

    $id = $CI->input->get('id');


Solution 9

n = is your last sagment of you url
$id = $this->uri->segment(n);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply