dougu0824 2015-09-25 06:25
浏览 35

无法使用angular.js和php在数据库中获取值

i am unable to fill database after post by the user using angular.js and PHP.After clicking on submit button the row is created inside MySQL database but no values is there.Please cehck my code below.

add_data.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="Add_Data">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD APPLICATION</title>
<script src="js/angularjs.js" type="text/javascript"></script>
<script src="js/create.js" type="text/javascript"></script>
<style>
#dv1{
     border:1px solid #DBDCE9; margin-left:auto;
     margin-right:auto;width:220px;
     border-radius:7px;padding: 25px;
}
.info{
     border: 1px solid;margin: 10px 0px;
     padding:10px;color: #00529B;
     background-color: #BDE5F8;list-style: none;
}
.err{
   border: 1px solid;  margin: 10px 0px;
   padding:10px;  color: #D8000C;
   background-color: #FFBABA;   list-style: none;
}
</style>
</head>

<body>
<div id='dv1'>
  <form ng-controller="FrmController">
                <ul>
                    <li class="err" ng-repeat="error in errors"> {{ error}} </li>
                </ul>
                <ul>
                    <li class="info" ng-repeat="msg in msgs"> {{ msg}} </li>
                </ul>
                <h2>Sigup Form</h2>
                <div>
                    <label>Name</label> 
                    <input type="text" ng-model="username" placeholder="User Name" style='margin-left: 22px;'> 
                </div>
                <div>
                    <label>Email</label>
                    <input type="text" ng-model="useremail" placeholder="Email" style='margin-left: 22px;'> 
                </div>
                <div>
                    <label>Password</label>
                    <input type="password" ng-model="userpassword" placeholder="Password">
                </div>
                <button ng-click='SignUp();' style='margin-left: 63px;margin-top:10px'>SignUp</button>
     </form>
  </div>
</body>
</html>

create.js:

var app=angular.module('Add_Data',[]);
app.controller('FrmController',function($scope,$http){
    $scope.errors = [];
    $scope.msgs = [];
    $scope.SignUp=function(){
        $scope.errors.splice(0, $scope.errors.length); // remove all error messages
        $scope.msgs.splice(0, $scope.msgs.length);

        $http.post('php/create.php', {'uname': $scope.username, 'pswd': $scope.userpassword, 'email': $scope.useremail}
               ).success(function(data, status, headers, config) {
                if (data.msg != '')
                    {
                        $scope.msgs.push(data.msg);
                    }
                    else
                      {
                         $scope.errors.push(data.error);
                      }
                    }).error(function(data, status) { // called asynchronously if an error occurs
// or server returns response with an error status.
                        $scope.errors.push(status);
         });
    }
});

create.php:

<?php
$data = json_decode(file_get_contents("php://input"));
$usrname = mysql_real_escape_string($data->uname);
$upswd = mysql_real_escape_string($data->pswd);
$uemail = mysql_real_escape_string($data->email);

$con = mysql_connect('localhost', 'root', '******');
mysql_select_db('AngularTest', $con);

$qry_em = 'select count(*) as cnt from users where email ="' . $uemail . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);

if ($res['cnt'] == 0) {
    $qry = 'INSERT INTO users (name,pass,email) values ("' . $usrname . '","' . $upswd . '","' . $uemail . '")';
    $qry_res = mysql_query($qry);
    if ($qry_res) {
        $arr = array('msg' => "User Created Successfully!!!", 'error' => '');
        $jsn = json_encode($arr);
        print_r($jsn);
    } else {
        $arr = array('msg' => "", 'error' => 'Error In inserting record');
        $jsn = json_encode($arr);
        print_r($jsn);
    }
} else {
    $arr = array('msg' => "", 'error' => 'User Already exists with same email');
    $jsn = json_encode($arr);
    print_r($jsn);
}

?>

When user is clicking of signup button one row is created inside the database but the three field is showing blank.I am also getting the message user created successfully on the page.Please help me to resolve this error.

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 聚类分析或者python进行数据分析
    • ¥15 逻辑谓词和消解原理的运用
    • ¥15 三菱伺服电机按启动按钮有使能但不动作
    • ¥15 js,页面2返回页面1时定位进入的设备
    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号