简体   繁体   English

将数据插入 PHP 中的 SQL 服务器中

[英]Insert data into SQL Server in PHP

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.从一个表单 (HTML) 中,我通过 AJAX 发送一个标志来指示操作和一个 object,其中包含要保存在 PHP 中的信息。

In the HTML file:在 HTML 文件中:

  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)在 PHP 文件 (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此代码仅返回 false,但不标记任何错误

If you want to return an error you need to call a method/function to output the error.如果你想返回一个错误,你需要调用一个方法/函数来 output 错误。 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在下面的评论之间,我使用的是https://www.php.net/manual/en/pdo.errorinfo.php ,它只返回与已运行的最后一个 ZAC5C74B64B4B8352EF2F18AZ1AFFB5AC2 语句相关的错误

$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....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM