Angular JS:通过JSON将行插入MySQL数据库不起作用

时间:2018-03-30 15:56:27

标签: php mysql angular ionic-framework

我正在使用Angular JS中的Ionic Framework开发一个针对移动设备的小应用程序。我试图在MySQL中添加一行 - 这不起作用。

question.ts:

   createEntry(questionid : Int32Array, questionanswer : Int32Array) : void
   {
      let headers   : any       = new HttpHeaders({ 'Content-Type': 'application/json' }),
          options   : any       = { "key" : "create", "user" : 'test', "questionid" : questionid, "questionanswer" : questionanswer },
          url       : any       = "http://www.so-ta.de/brkwrkt2018/manage-data.php";

      this.http.post(url, JSON.stringify(options), headers)
      .subscribe((data : any) =>
      {

         this.sendNotification(`Answer for ${questionid} added`);
      },
      (error : any) =>
      {
        this.sendNotification(`Error`);
      });
   }

管理-data.php:

 switch($key)
   {

      case "create":

         $user              = filter_var($obj->user, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
         $questionid        = filter_var($obj->questionid, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
         $questionanswer    = filter_var($obj->questionanswer, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);

         try {
            $sql    = "INSERT INTO fragen_antworten(user, questionid, questionanswer) VALUES(:user, :questionid, :questionanswer)";
            $stmt   = $pdo->prepare($sql);
            $stmt->bindParam(':user', $user, PDO::PARAM_STR);
            $stmt->bindParam(':questionid', $questionid, PDO::PARAM_STR);
            $stmt->bindParam(':questionanswer', $description, PDO::PARAM_STR);
            $stmt->execute();

            echo json_encode(array('message' => 'Congratulations the record user: ' . $user . ' | questionid' . $questionid . ' | questionanswer ' . $questionanswer . ' was added to the database'));
         }
         catch(PDOException $e)
         {
            echo $e->getMessage();
         }

      break;
   }

给定值questionid和questionanswer都可以正确打印出来。我的问题是:我如何得到一条错误信息,帮助我找到问题?是php文件还是js文件? :-( Heeeelp me

1 个答案:

答案 0 :(得分:0)

您的代码不会抛出,因此永远不会返回您的错误。

$stmt->execute()将返回true或false,具体取决于是否成功。如果为false,您可以使用$pdo->errorInfo()收到错误消息。