doulu4316 2017-03-30 10:46
浏览 92

使用angular,php,mysql进行数据库连接

Hi I am having some trouble with a small project. It's my first time using mySQL and PHP so I'm just trying to get connectivity for now. I have followed some tutorials and I cannot connect to the database, I am getting the "Sorry! Data Couldnt be inserted!" error from my php script at the bottom of index.html.

I also have a password on the database for the root user however even when i type this in the php file it still wont work.

Any help is much appreciated guys.

index.html:

 <!DOCTYPE html>
<html>
  <head>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></link>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  </head>
  <body>
      <div class= "container" style = "width:500px">
      <div ng-app="myapp" ng-controller="usercontroller">
            <label>Email</label>
            <input type ="text" name="email" ng-model="email" class="form-control"/>
             <br />
             <label>Password</label>
            <input type ="text" name="password" ng-model="password" class="form-control"/>
            <br /> 
            <input type="submit" name="btnInsert" ng-click="insertData()" value="ADD"/>
      </div>
      </div>



 <script>
   var app = angular.module('myapp',[]);

   app.controller('usercontroller', function($scope, $http){
      $scope.insertData = function(){
        $http.post(
          "insert.php",{
            'email':$scope.email,
            'password':$scope.password
          }).then(function(response){
            alert("Data inserted Successfully! YIPEEEE!!!");
            console.log("Data inserted Successfully");
        },function(error){
          alert("Sorry! Data Couldnt be inserted!");
          console.error(error);
        });
      }
   });
  </script>
  </body>
 </html>

and my insert.php:

<?php
//insert.php


$data = json_decode(file_get_contents("php://input"));


    $email =  $data->email;
    $password = $data->password;

    mysql_connect("localhost","root","");
    mysql_select_db("test");

    mysql_query ("INSERT INTO 'test_user'('email', 'password') VALUES ('".$email."','".$password."')") or die(mysql_error());

    echo $email. " " .$password;

/*
    if(mysqli_query($connect, $query)){
        echo "User inserted...";
    }
    else{
        echo "User not inserted...";
    }
*/

#mysqli_real_escape_string($connect,
#mysqli_real_escape_string($connect, 
?>

</div>
  • 写回答

1条回答 默认 最新

  • dongqing904999 2017-03-30 11:46
    关注

    It's my first time using mySQL and PHP

    But you seem to be using a very old version of PHP (the mysql_ extension is deprecated).

    I'm not familiar with Angular but it looks like your code relies on Angular to determine if the PHP script ran as intended. Angular can't base that decision on the content sent by your script, therefore it must be basing its decision on the HTTP status returned by your script (which has no debugging in it). I expecting your PHP code is crashing and burning before it gets to run the insert.

    I suggest you:

    1) build a test rig using a textare and NO javascript to fire a request at your insert.php script

    2) amend your PHP script so that it - captures more information about the processing - uses appropriate error reporting and ensure your error logging is working - presents the (appropriate) details of the actions it took in a parseable format (i.e. JSON)

    3) read your log files - for both PHP and MySQL (you might want to do this before going to the trouble of changing/verifying your logging as described above)

    4) read up on SQL injection

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图