Search code examples
phpajaxmysqliinternal-server-error

500 (Internal Server Error) while using ajax codeigniter


I am getting error jquery.min.js:4 POST http://localhost/Speakertest/index.php/Welcome/ajax 500 (Internal Server Error)

I am calling ajax function in codeigniter

after adding query I am getting error https://i.sstatic.net/AfxVd.png

this is my ajax function code

    public function ajax()
{



    $size="";
    $eprice="";

    $size = $this->input->post('size');
    $sprice = $this->input->post('sprice');
    $eprice = $this->input->post('eprice');

    var_dump($size);
    var_dump($sprice);
    var_dump($eprice);


      $query = $this->db->query("SELECT * from info_user Where user_status ='1'"); 

                  if(!empty($size)){

                      $query  .= $this->db->query(" and city in('$size')"); 
                  }

                  if(!empty($sprice) && !empty($eprice)){

                      $query  .=  $this->db->query(" and charge_per_hour >='$sprice' and charge_per_hour <='$eprice'"); 
                  }

                foreach( $result as $row )
                {

                            echo $row->name; 
                            echo $row->charge_per_hour; 
                            echo $row->city; 

                 }
}

this is the lines which generates error

$query = $this->db->query("SELECT * from info_user Where user_status ='1'"); 

                  if(!empty($size)){

                      $query  .= $this->db->query(" and city in('$size')"); 
                  }

                  if(!empty($sprice) && !empty($eprice)){

                      $query  .=  $this->db->query(" and charge_per_hour >='$sprice' and charge_per_hour <='$eprice'"); 
                  }

                foreach( $result as $row )
                {

                            echo $row->name; 
                            echo $row->charge_per_hour; 
                            echo $row->city; 

                 }

and this is my ajax call

 $.ajax({
            url:"http://localhost/Speakertest/index.php/Welcome/ajax",
            type:'post',
            data:{size:size,sprice:ui.values[ 0 ],eprice:ui.values[ 1 ]},
            success:function(result){
                $('.product-data').html(result);
            }
        });

after removing that mysql query it working fine. how to rid that POST http://localhost/Speakertest/index.php/Welcome/ajax 500 (Internal Server Error)


Solution

  • Change your code with this

        $query = "SELECT * from info_user Where user_status ='1'"; 
        if(!empty($size)){
            $query  .= " and city in('".$size."')"; 
        }
        if(!empty($sprice) && !empty($eprice)){
            $query  .=  " and charge_per_hour >='".$sprice."' and 
            charge_per_hour <='".$eprice."'"; 
        }
    
         $result = $this->db->query($query);
    
         foreach( $result as $row )
         {
             echo $row->name; 
             echo $row->charge_per_hour; 
             echo $row->city; 
         }