0

From a form (HTML), I send via AJAX a flag to indicate the action and a object that contains the information to save in PHP.

In the HTML file:

  function Mandar_DATOS(QueHacer,OBJEvento){

//          alert (OBJEvento.IdPaciente);
        $.ajax({
          type:'POST', 
          url:'./php/eventos.php?QueHacer='+QueHacer,
          data:OBJEvento,success:function(msg){
              if(msg){
                       .//mostrar en pantalla la informacion
              }
          },error:function(){
              alert("No se guardo...");
          }
      });
  }

in the PHP file (eventos.php)

$la_conexion =  Conexion::ObtenConexion();

$QueHacer=(isset($_GET['QueHacer']))?$_GET['QueHacer']:'LEER';

switch ($QueHacer){
    case 'GUARDAR':

        $CadenaSQL=$la_conexion->prepare("INSERT INTO "
      . "AgendaVideo(id, IdPaciente, IdMedico, title, start, end, color, textColor) "
          . "VALUES(:id, :IdPaciente, :IdMedico, :title, :start, :end, :color, :textColor)");        

        $RESULTADO=$CadenaSQL->execute(array(
            "id"=>$_POST['id'], 
            "IdPaciente"=>$_POST['IdPaciente'], 
            "IdMedico"=>$_POST['IdMedico'], 
            "title"=>$_POST['title'], 
            "start"=>$_POST['start'], 
            "end"=>$_POST['end'], 
            "color"=>$_POST['color'], 
            "textColor"=>$_POST['textColor']
        ));

        echo json_encode($RESULTADO);
        break;
    case....

this code only returns false, but does not mark any error

Dale K
  • 25,246
  • 15
  • 42
  • 71
Alejandro
  • 3
  • 3

1 Answers1

0

If you want to return an error you need to call a method/function to output the error. Here below between the comments I'm using https://www.php.net/manual/en/pdo.errorinfo.php which just returns the error related to the last sql statement that has been run

$la_conexion =  Conexion::ObtenConexion();

$QueHacer=(isset($_GET['QueHacer']))?$_GET['QueHacer']:'LEER';

switch ($QueHacer){
    case 'GUARDAR':

        $CadenaSQL=$la_conexion->prepare("INSERT INTO "
      . "AgendaVideo(id, IdPaciente, IdMedico, title, start, end, color, textColor) "
          . "VALUES(:id, :IdPaciente, :IdMedico, :title, :start, :end, :color, :textColor)");        

       //start error handling code
       if (!$CadenaSQL) {
           echo "\nPDO::errorInfo():\n";
           print_r($la_conexion->errorInfo());
       }
       //end error handling code

        $RESULTADO=$CadenaSQL->execute(array(
            "id"=>$_POST['id'], 
            "IdPaciente"=>$_POST['IdPaciente'], 
            "IdMedico"=>$_POST['IdMedico'], 
            "title"=>$_POST['title'], 
            "start"=>$_POST['start'], 
            "end"=>$_POST['end'], 
            "color"=>$_POST['color'], 
            "textColor"=>$_POST['textColor']
        ));

       //start error handling code
       if (!$RESULTADO) {
           echo "\nPDO::errorInfo():\n";
           print_r($la_conexion->errorInfo());
       }
       //end error handling code

        echo json_encode($RESULTADO);
        break;
    case....
namrogom
  • 155
  • 9
  • throws the following to me: PDO::errorInfo(): Array ( [0] => 00000 [1] => [2] => ) false what does this mean? – Alejandro May 26 '20 at 19:28
  • see the answers here https://stackoverflow.com/questions/11813911/php-pdo-error-number-00000-when-query-is-correct – namrogom May 26 '20 at 21:16