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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配