繁体   English   中英

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

[英]Angular JS: Inserting Row into MySQL Database by JSON doesnt work

我在一个用于移动设备的小应用程序上使用Angular JS中的Ionic框架。 我试图在MySQL中添加一行-不起作用。

问题:

   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`);
      });
   }

manage-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我

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

$stmt->execute()将返回true或false,具体取决于是否成功。 如果为false,则可以通过$pdo->errorInfo()获得错误消息。

暂无
暂无

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

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