0

Im getting a NULL return for the last insert id, I would appreciate if someone could check my code. I have placed an id number in place of the $result query and it returns the json echo. My hunch is that it has something to do with the placement of the last "}" bracket.

if (empty($_POST) === false) {
$update_data = array(
      'company_id'              => $_POST['company_id'],
      'addpatient_firstname'    => $_POST['addpatient_firstname'],
      'addpatient_lastname'     => $_POST['addpatient_lastname'],
      'addpatient_dob'          => $_POST['addpatient_dob'],
      'patient_added'           => $_POST['patient_added']
    );


    $required_fields = array('company_id', 'addpatient_firstname', 'addpatient_lastname', 'addpatient_dob', 'patient_added');
    foreach($update_data as $key=>$value) {
        if (empty($value) && in_array($key, $required_fields) === true) {
            $errors[] = 'Fields marked with an asterisk are required';
            break 1;
        }
    }
}

if (empty($_POST) === false && empty($errors) === true) {

      $company_id   = $_POST['company_id'];
      $patient_id   = $_POST['addpatient_id'];
      $first_name   = $_POST['addpatient_firstname'];
      $last_name    = $_POST['addpatient_lastname'];
      $dob          = $_POST['addpatient_dob'];
      $updated      = $_POST['patient_added'];


      $update = array();
      array_walk($update_data, 'array_sanitize');

      foreach($update_data as $field=>$data) {
          $update[] = '`' . $field . '` = \'' . $data . '\'';
      }

      mysql_query("INSERT INTO `lab`.`patients` (`company_id`, `patient_id`, `patient_firstname`, `patient_lastname`, `patient_dob`, `patient_added`) VALUES ('$company_id', NULL, '$first_name', '$last_name', '$dob', '$updated')");
      $last_patient_id = mysql_insert_id();

      $result = mysql_query("SELECT `patient_id`, `patient_firstname`, `patient_lastname`, `patient_dob` FROM `patients` WHERE `patient_id` = $last_patient_id");

      $data[] = mysql_fetch_assoc($result);
}

      echo json_encode( $data );

/*        exit();

} else if (empty($errors) === false) {
echo output_errors($errors);
}*/
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Adam
  • 297
  • 6
  • 20
  • 1
    Are the records being inserted properly? – andrewsi Sep 21 '12 at 14:22
  • Check the result of `mysql_query` first. (the insert one) – xdazz Sep 21 '12 at 14:24
  • @andrewsi The insert does seem to work properly. – Adam Sep 21 '12 at 14:28
  • @xdazz The insert does seem to work properly. – Adam Sep 21 '12 at 14:28
  • 1
    @Adam - Could you try the insert, but without the patient_id part? If it's auto_increment, you don't need to have it in your INSERT. – andrewsi Sep 21 '12 at 14:29
  • @andrewsi I removed the patient_id and NULL in the insert, and the insert still functions, but Im still not getting any json echo. – Adam Sep 21 '12 at 14:40
  • @Adam - can you echo out your SELECT query, and run it directly in the database, in that case? Also, I note that the INSERT uses the table `lab.patients` while the SELECT uses `patients` - does that make a difference? – andrewsi Sep 21 '12 at 14:42

2 Answers2

0

If patient_id is auto_increment field, then you should leave this field out of the insert statement, instead of inserting NULL.

INSERT INTO `lab`.`patients` (`company_id`, `patient_firstname`, `patient_lastname`, `patient_dob`, `patient_added`) VALUES ('$company_id', '$first_name', '$last_name', '$dob', '$updated')
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • With your update, the INSERT still works, but Im still not getting the expected json echo. – Adam Sep 21 '12 at 14:44
  • @Adam Do you get the right id, right data? try do some debug. – xdazz Sep 21 '12 at 14:45
  • No, Im not getting anything back. The insert works properly, but the $last_patient_id is not getting passed to the $result = mysql_query. Im getting a mysql_fetch_assoc() expects para 1. Ive replaced the mysql_insert_id with an id number that exists, but its not getting passed to the $result query. – Adam Sep 21 '12 at 14:57
  • I have to step out for 2 hours, then Ill be back, really appreciate your help! – Adam Sep 21 '12 at 15:00
0

I see in the www.php.net that the mysql_insert_id() only works with auto incremented columns. How is your primary key ?

Nicole Stutz
  • 516
  • 4
  • 15