title: Getting 500 Internal Server Error with jQuery AJAX and CodeIgniter
body: <p>I'm trying to submit a form (it's a dynamic form with fields added via jQuery) to CodeIgniter for a db insert. Part of it works, the other part doesn't.</p>
  
  <p><strong>Here's the jQuery:</strong></p>
  
  <pre><code>function submitForm() {
      $.ajax({
          type: 'POST',
          url: '/raffle/save/',
          data: $('#raffle').serialize(),
          success: function (response) {
              alert(response);
          },
          error: function() {
              alert('Failed'); // This is what I get unless I comment out the entry insert
          }
      });
  }
  </code></pre>
  
  <p><strong>The CI controller:</strong></p>
  
  <pre><code>class Raffle extends CI_Controller {
      public function __construct() {
          parent::__construct();
          $this-&gt;load-&gt;model('raffle_model');
          $this-&gt;load-&gt;library('form_validation');
      }
  
      public function index() {
          $data['title'] = 'Create a Raffle';
          $this-&gt;load-&gt;view('header', $data);
          $this-&gt;load-&gt;view('raffles/create_view', $data);
          $this-&gt;load-&gt;view('raffles/bottombar_view', $data);
          $this-&gt;load-&gt;view('footer', $data);
      }
  
      public function save() {
          foreach($_POST as $k =&gt; $v) {
              if($k == 'entrant' || $k == 'tickets') {
                  foreach ($v as $i =&gt; $vector) {
                      $this-&gt;form_validation-&gt;set_rules('entrant[' . $i . ']', 'Entrant name', 'trim|required|min_length[1]|max_length[100]|xss_clean');
                      $this-&gt;form_validation-&gt;set_rules('tickets[' . $i . ']', 'Number of tickets', 'trim|required|max_length[2]|is_natural_no_zero|xss_clean');
                  }
              } else {
                  $this-&gt;form_validation-&gt;set_rules('raffle-name', 'Raffle name', 'trim|required|min_length[4]|max_length[100]|xss_clean');
                  $this-&gt;form_validation-&gt;set_rules('winners', 'Number of winners', 'trim|required|max_length[2]|is_natural_no_zero|xss_clean');
              }
          }
  
          if($this-&gt;form_validation-&gt;run() == FALSE) {
              echo 'Validation failure!';
          } else {
              if($this-&gt;raffle_model-&gt;add_raffle()) { // It does pass validation and goes to the model
                  echo 'Data added successfully!';
              }
          }
      }
  }
  </code></pre>
  
  <p>And the <strong>CI model:</strong></p>
  
  <pre><code>class Raffle_model extends CI_Model {
      public function __construct() {
          parent::__construct();
      }
  
      public function add_raffle() {
          // This works
          $meta = array(
              'user_id'    =&gt; $this-&gt;session-&gt;userdata('user_id'),
              'name'       =&gt; $this-&gt;input-&gt;post('raffle-name'),
              'winners'    =&gt; $this-&gt;input-&gt;post('winners'),
              'created_ip' =&gt; $_SERVER['REMOTE_ADDR']
          );
  
          // This works and is a multidimensional array for insert_batch()
          $entrants = array(
              array(
                  'date'      =&gt; date(DATE_ATOM),
                  'raffle_id' =&gt; '1'
              )
          );
  
          foreach($_POST['entrant'] as $name =&gt; $n) {
              array_push($entrants,
                  array(
                      'name'    =&gt; $n,
                      'tickets' =&gt; $_POST['tickets'][$name]
                  )
              );
          }
  
          $this-&gt;db-&gt;insert('raffle', $meta);
          $this-&gt;db-&gt;insert_batch('entry', $entrants); // This one returns error 500
  
          return true;
      }
  }
  </code></pre>
  
  <p><strong>Here's the problem:</strong> Submitting the form, the <code>meta</code> portion does get saved to the <code>raffle</code> table, but the <code>entrants</code> portion does not get saved to the <code>entry</code> table. I have tried using a simple dummy array (sample data, no post data, no loops) to see if it would work, but it still doesn't. Console logs say <code>POST http://rafflegrab.dev/raffle/save/ 500 (Internal Server Error)</code>.</p>
  
  <p>CSRF is off in CI config.</p>
  
  <p>The table is set up as follows:</p>
  
  <ol>
  <li>Table name: entry</li>
  <li>InnoDB</li>
  <li>id - bigint(12) - UNSIGNED - not_null - AUTO_INCREMENT - PRIMARY</li>
  <li>user_id - int(10) - UNSIGNED </li>
  <li>name - varchar(100)</li>
  <li>tickets - smallint(5) - UNSIGNED</li>
  <li>date - datetime</li>
  <li>raffle_id - int(10) - UNSIGNED</li>
  </ol>