0

I am trying to pass data to a php page via ajax, the data gets inserted to the database, then I need to pick up the last insert and pass the back to update a select menu with that last insert selected. The database gets updated correctly, but Im getting a NULL return for the echo json_echo($data);

Been stuck on this all day, would really appreciate the help!!!

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_firstname`, `patient_lastname`, `patient_dob`, `patient_added`) VALUES ('$company_id', '$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 );
Adam
  • 297
  • 6
  • 20
  • 2
    Possible answer: http://stackoverflow.com/questions/1972006/json-encode-is-returning-null – Matthew Blancarte Sep 21 '12 at 22:37
  • 3
    Check what's inside your var with `var_dump($data)` as a first thing. – moonwave99 Sep 21 '12 at 22:37
  • @moonwave99 var_dump returns NULL. I believe mysql_insert_id is not picking up the last insert id. The database is getting inserted correctly, but this has been driving me crazy all day. If I move the last "}" bracket above the INSERT I get the json_encode I am expecting. Any thoughts? – Adam Sep 21 '12 at 22:43
  • @MatthewBlancarte the database is encoded properly. Any thoughts? – Adam Sep 21 '12 at 22:47
  • 1
    My next guess is that your conditional is evaluating to false. var_dump($_POST, $errors). Also, you do not need to use any comparison with empty(). Just do: if( !empty($_POST) && !empty($errors) ) – Matthew Blancarte Sep 21 '12 at 22:50
  • Well, if `$data` is empty, what's in `$result` for each loop? And if that's empty, then what's in `$last_patient_id`? – ernie Sep 21 '12 at 22:51
  • And the standard disclaimer about the `mysql_*` methods being deprecated . . . – ernie Sep 21 '12 at 22:53
  • I agree with @MatthewBlancarte, fix your boolean zen, ie, dont do false === false type of stuff. I do not thing that is your problem though because it wouldn't run the insert and since you say the database is being updated we know it is entering the if statement – Phil Sep 21 '12 at 22:54
  • @Adam check `$last_patient_id` value: is it set? If so, have you got such record in your db? There lies the problem. – moonwave99 Sep 21 '12 at 22:57
  • @Adam What is the result of var_dump($_POST, $errors);? Also, inside of your conditional, just do var_dump( 'got here' ); to see how far this program goes. – Matthew Blancarte Sep 21 '12 at 23:00
  • @moonwave99 when I run the page without the conditional if statement, the database get filled with a blank entry but it auto increments and then returns `[{"patient_id":"16","patient_firstname":"","patient_lastname":"","patient_dob":"0000-00-00"}]` – Adam Sep 21 '12 at 23:11
  • @MatthewBlancarte Thank you, I updated my code. Is that just a better way to code the conditional statement? – Adam Sep 21 '12 at 23:14

1 Answers1

1

json_encode returns false if an error happened (php manual). I would start there.

$json_string = json_encode( $data );
if( $json_string ){
   echo $json_string;
}else{
   echo "Error";
   echo "<pre>";
   print_r($data);
   echo "</pre>";
}

That should at least lead you a way to debug.

EDIT: Also try add this to the beginning of the function all

error_reporting(E_ALL); 
ini_set('display_errors', '1'); 

This will help display errors that the mysql is throwing.

EDIT: I wanted to just fix spelling, but since I need 6 characters minimum I will mention http://jsonlint.com/ to validate what you're putting into json_encode

Phil
  • 410
  • 3
  • 12
  • I get a return of [null]. I think the mysql_insert_id is not picking up the last insert, which in turn is not coming back with any query result. Any thoughts? – Adam Sep 21 '12 at 22:55
  • you sure it is AUTO_INCREMENTING right? if you add error_reporting(E_ALL); ini_set('display_errors', '1'); to the php at the beginning of the function call it might display a helpful error – Phil Sep 21 '12 at 22:56