-1

May I know why I'm getting this error message (500 (Internal Server Error)) on the console. And nothing appears on the table. I'd like to display data on a table from the information that will be sent using JavaScript

/* MODEL */

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');


    class User extends CI_Model
    {

        function __construct() {
            parent::__construct();
            $this->load->database();       
        }

        public function validate_user($email, $password) {
            $this->db->distinct();
            $this->db->select('c.contact_id, name, email, password, level, c.statusid');
            $this->db->from('contact c');
            $this->db->join('deliveryreg d', 'c.contact_id = d.idcontact');
            $this->db->join('ClientLogin cl', 'cl.idcontact = c.contact_id');
            $this->db->where('c.statusid = 1');
            $this->db->where('email', $email);
            $this->db->where('password', $password); 
            $this->db->limit(1); 

            $query = $this->db->get();

           if($query->num_rows() == 1)
           {
             return $query->result();
           }
           else
           {
             return false;
           }
            }



        public function getSearch($data){


            $this->db->distinct();
            $this->db->select(" s.id AS 'searchid',s.description as 'searchname',so.id AS 'sourceid',so.description as 'service'");
            $this->db->from('contact c');
            $this->db->join('deliveryreg d', 'c.contact_id = d.idcontact');
            $this->db->join('searchsource ss', 'ss.id = d.idsearchsource');
            $this->db->join('search s', 's.id = ss.idsearch');
            $this->db->join('source so', 'so.id = ss.idsource');
            $this->db->where('c.statusid = 1'); 
            $this->db->where('email', $data['email']);
            $ids = array('1','5','8');
            $this->db->where_in('so.id', $ids);
            $this->db->order_by('s.description', 'so.description');

            $query = $this->db->get(); 
            return $query->result();
        }

        public function printData(){

            $db2 = $this->load->database('otherdb', TRUE);
            $db3 = $this->load->database('pan', TRUE);

            $sql = "SELECT c.id,p.name AS 'Pubname',section,pubdate,byline,headline,subheadline,TEXT,AREA,ave,linkedto,thumimage,pagenum,clipuploadpath, c.articleref,pdf,docpath,articlepartnumber,articlepartscount,keyword,logo,p.description AS 'PubInfo',circulation,m.description AS 'MediaType', r.description FROM pearhpmszm_db5.Allocation a JOIN pearhpmszm_db5.clip c ON c.id = a.idclip LEFT JOIN pearhpmszm_db3.Publication p ON c.pubid = p.id LEFT JOIN pearhpmszm_db3.ABC ab ON ab.idpub = p.id
                LEFT JOIN pearhpmszm_db3.mediatype m ON m.id = p.idmediatype LEFT JOIN pearhpmszm_db3.Region r ON r.id = p.idregion WHERE a.idsearch IN ('".'$_POST["idsearch"]'."') AND c.pubdate BETWEEN '".'$_POST["dateFrom"]'."' AND '".'$_POST["dateTo"]'."'";

            $query = $this->db->query($sql);
            return $query->result();

        }

        public function onlineData(){

            $db2 = $this->load->database('otherdb', TRUE);
            $db4 = $this->load->database('pan2', TRUE);

            $sql = "SELECT a.idsearch AS 'Searchid',c.id AS 'clipid',c.url,headline,c.description,pubdate,author,TEXT,ave,p.name AS 'sitename', st.description AS 'sitetype',r.description AS 'region' FROM pearhpmszm_db4.allocation a JOIN pearhpmszm_db4.clip c ON c.id = a.idclip LEFT JOIN pearhpmszm_db3.site p ON c.siteid = p.id LEFT JOIN pearhpmszm_db3.sitetype st ON st.id = p.sitetypeid LEFT JOIN pearhpmszm_db3.Region r ON r.id = p.regionid WHERE a.idsearch IN ('".'$Searchid'."') AND c.pubdate BETWEEN '".'$dateFrom'."' AND '".'$dateTo'."' ";

            $query = $this->db->query($sql);
            return $query->result();

        }

        public function broadcastData(){

            $db2 = $this->load->database('otherdb', TRUE);
            $db5 = $this->load->database('pan3', TRUE);

            $sql = "SELECT mentions.id,`pearhpmszm_db2`.`search`.Description,pearhpmszm_db6.Allocation.idsearch,Mention,station,MentionDT,duration,AVE,clipPath,  au.Audience,pearhpmszm_db3.Region.Description AS 'Region',pearhpmszm_db3.language.language,pearhpmszm_db6.station.idlanguage,pearhpmszm_db6.licenceType.Licence,  (SELECT programme FROM  pearhpmszm_db6.schedule WHERE  idstation = pearhpmszm_db6.station.id AND CASE DAYOFWEEK(mentiondt) WHEN 1 THEN  sun = 1  WHEN 2 THEN  mon = 1  WHEN 3 THEN  tue = 1  WHEN 4 THEN  wed = 1  WHEN 5 THEN  thu = 1 WHEN 6 THEN  fri = 1   WHEN 7 THEN  sat = 1 END AND starttime < TIME(mentiondt) AND endtime > TIME(mentiondt) LIMIT 1 ) AS Programme, pearhpmszm_db6.Allocation.keyword FROM pearhpmszm_db6.Allocation  JOIN pearhpmszm_db6.mentions ON pearhpmszm_db6.Allocation.idmention = pearhpmszm_db6.mentions.ID   JOIN `pearhpmszm_db2`.`search` ON `pearhpmszm_db6`.`Allocation`.idsearch = `pearhpmszm_db2`.`search`.id  LEFT JOIN pearhpmszm_db6.station ON pearhpmszm_db6.station.name = pearhpmszm_db6.mentions.Station  LEFT JOIN pearhpmszm_db3.Audience au  ON au.idstation = `pearhpmszm_db6`.station.id AND au.active = 1  LEFT JOIN pearhpmszm_db3.Region ON pearhpmszm_db6.station.IDRegion = pearhpmszm_db3.Region.ID   LEFT JOIN pearhpmszm_db3.language ON pearhpmszm_db6.station.idlanguage = pearhpmszm_db3.language.id   LEFT JOIN pearhpmszm_db6.licenceType ON pearhpmszm_db6.licenceType.id = pearhpmszm_db6.station.idlicencetype   LEFT JOIN pearhpmszm_db6.programname  ON au.idstation = `pearhpmszm_db6`.station.id   WHERE pearhpmszm_db6.Allocation.idsearch IN ('".'$idsearch'."') AND pearhpmszm_db6.mentions.MentionDT BETWEEN '".'$dateFrom'."' AND '".'$dateTo'."'";

            $query = $this->db->query($sql);
            return $query->result();

        }

        function __destruct() {
                $this->db->close();
            }
    }

/* CONTROLLER */

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class PrintData extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('user');

 }

 function index()
 {
   if($this->session->userdata('logged_in'))
   {
     $session_data = $this->session->userdata('logged_in');
     $data['email'] = $session_data['email'];
     $data['query'] = $this->user->getSearch($data);
     $data['print'] = $this->user->printData($data);
     $this->load->view('header', $data);
     $this->load->view('sidebar', $data);
     $this->load->view('print_content', $data);
     $this->load->view('footer', $data);
   }
   else
   {
     //If no session, redirect to login page
     redirect('login', 'refresh');
   }
 }

 function getPrintData($data){
  if($this->session->userdata('logged_in'))
   {
       $session_data = $this->session->userdata('logged_in');
       $data['email'] = $session_data['email'];
       $data['idsearch'] = $this->user->printData($data);
       $data['dateFrom'] = $this->user->printData($data);
       $data['dateTo'] = $this->user->printData($data);
       $data['query'] = $this->user->getSearch($data);
       $data['print'] = $this->user->printData($data);

   }
   else
   {
     //If no session, redirect to login page
     redirect('login', 'refresh');
   }
 }

 function logout()
 {
   $this->session->sess_destroy('logged_in');
   session_destroy();
   redirect('home', 'refresh');
 }

}

?>

/* VIEW */

      <!-- Add the sidebar's background. This div must be placed
           immediately after the control sidebar -->
      <!-- <div class="control-sidebar-bg"></div> -->
    </div><!-- ./wrapper -->

    <!-- REQUIRED JS SCRIPTS -->

    <!-- jQuery 2.1.4 -->
    <script src="<?php echo base_url();?>assets/js/jQuery-2.1.4.min.js"></script>
    <!--Jquery-->
    <script src="<?php echo base_url();?>assets/js/jquery.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script>
    <!-- AdminLTE App -->
    <script src="<?php echo base_url();?>assets/js/app.min.js"></script>
    <!-- Scroll effect -->
    <script src="<?php echo base_url();?>assets/js/scroll.js"></script>
    <!-- Slide effect -->
    <script src="<?php echo base_url();?>assets/js/slideEffect.js"></script>

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>

    <script>


      /*******************************************
          GET THE LAST 24HRS DATA PER SEARCHID
      ********************************************/

      function getPrintData(){
        alert("In getPrintData");

          var dateFrom = moment().add(-1,'days').format("DD/MM/YYYY");
          var dateTo = moment().format("DD/MM/YYYY");
          var searchid = "";
          var count = 0;
            searchid = $('.searchid:checked').map(function () {
                count = count + 1;
                return this.value;
            }).get().join(",");


          $.ajax({
            url:"<?php echo site_url('printdata/getPrintData');?>",
            data:{dateFrom:dateFrom, dateTo:dateTo, idsearch:searchid},
            type:"POST",
            cache: false,
            success: function (response, status, xhr) {
                        alert(response);
                        $("#myContentPrint").html(response);

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(errorThrown);
                    }
          });
        }





    </script>



    <!-- Optionally, you can add Slimscroll and FastClick plugins.
         Both of these plugins are recommended to enhance the
         user experience. Slimscroll is required when using the
         fixed layout. -->
  </body>
</html>
2
  • your CSRF Security is Disable or not..........$config['csrf_protection'] = FALSE; Commented Jul 12, 2016 at 11:01
  • In your controller, change function name like this function getPrintData(). In your javascript, your are making post call to your getPrintData() function. You have to use $this->input->post() to retrieve data. Commented Jul 12, 2016 at 11:03

2 Answers 2

0

First capture data send by ajax in controller like this

  function getPrintData(){
    if($this->session->userdata('logged_in'))
      {
           $data = array('dateFrom' => $this->input->post('dateFrom'),
                 'dateTo' => $this->input->post('dateTo'),
                 'idsearch' => $this->input->post('idsearch'),
           );

       $session_data = $this->session->userdata('logged_in');
       $data['email'] = $session_data['email'];
       $data['idsearch'] = $this->user->printData($data);
       $data['dateFrom'] = $this->user->printData($data);
       $data['dateTo'] = $this->user->printData($data);
       $data['query'] = $this->user->getSearch($data);
       $data['print'] = $this->user->printData($data);

     }
     else
    {
        //If no session, redirect to login page
        redirect('login', 'refresh');
    }
  }

Second Your model should recieve data using argument passed to model method

but you didn't pass any argument

it should be like this:

  function printData($data = array()){

        $db2 = $this->load->database('otherdb', TRUE);
        $db3 = $this->load->database('pan', TRUE);

        $sql = "SELECT c.id,p.name AS 'Pubname',section,pubdate,byline,headline,subheadline,TEXT,AREA,ave,linkedto,thumimage,pagenum,clipuploadpath, c.articleref,pdf,docpath,articlepartnumber,articlepartscount,keyword,logo,p.description AS 'PubInfo',circulation,m.description AS 'MediaType', r.description FROM pearhpmszm_db5.Allocation a JOIN pearhpmszm_db5.clip c ON c.id = a.idclip LEFT JOIN pearhpmszm_db3.Publication p ON c.pubid = p.id LEFT JOIN pearhpmszm_db3.ABC ab ON ab.idpub = p.id  LEFT JOIN pearhpmszm_db3.mediatype m ON m.id = p.idmediatype LEFT JOIN pearhpmszm_db3.Region r ON r.id = p.idregion WHERE a.idsearch IN ('".'$data["idsearch"]'."') AND c.pubdate BETWEEN '".'$data["dateFrom"]'."' AND '".'$data["dateTo"]'."'";

        $query = $this->db->query($sql);
        return $query->result();

       }

and instead of using $_POST use $data in printData($data = array())

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks but it is still prompting the error message (500 INTERNAL SERVER ERROR)
can u echo the $sql to see what output it has
The $sql is perfect. It displays the data when I put the searchid, dateFrom and dateTo manually.
don't put it manually this is what i want to check whether data is captured by the model or not , want to see $data is replacing its corresponding value or not
I tried to echo the query but looks like data is not captured
|
0

First get data in controller:

function getPrintData(){
    if($this->session->userdata('logged_in'))
      {
           $data = array('dateFrom' => $this->input-post('dateFrom'),
                 'dateTo' => $this->input-post('dateTo'),
                 'idsearch' => $this->input-post('idsearch'),
           );
        $this->user->printData($data);

       $session_data = $this->session->userdata('logged_in');
       $data['email'] = $session_data['email'];
       $data['idsearch'] = $this->user->printData($data);
       $data['dateFrom'] = $this->user->printData($data);
       $data['dateTo'] = $this->user->printData($data);
       $data['query'] = $this->user->getSearch($data);
       $data['print'] = $this->user->printData($data);

     }
     else
    {
        //If no session, redirect to login page
        redirect('login', 'refresh');
    }
  }

you will get data in model :

public function printData($data){
//your code here
}

3 Comments

Thanks but still prompting the error message (500 internal server error)
you can check whether data is post or not by generating profiler report by adding $this->output->enable_profiler(true); in controller....and check view file below report
Data is not posted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.