使用php将angularjs表单值插入数据库

时间:2016-07-18 10:03:49

标签: php mysql angularjs

我创建了angularjs表单。我想使用PHP将表单值存储到数据库中,在插入之前我想检查天气是否已存在电子邮件。我是PHP的新手。任何帮助,将不胜感激。感谢。

Register.html:



<div class="container col-lg-10" style="margin-top:2em; margin-left:2em;" >
    <div class="panel panel-default">
     <div class="panel-body" ng-app="TempleWebApp" ng-controller="RegisterCtrl">

       <form name="userForm" ng-submit="submitForm()" novalidate>

                <!-- NAME -->
                <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid && (userForm.name.$dirty || submitted)}">
                    <label>Name</label>
                    <input type="text" name="name" class="form-control" ng-model="user.name" placeholder="Your Name" ng-required="true">
                    <p ng-show="userForm.name.$error.required && (userForm.name.$dirty || submitted)" class="help-block">You name is required.</p>
                </div>

                  <!-- EMAIL -->
                <div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && (userForm.email.$dirty || submitted)}">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" ng-model="user.email" placeholder="Your Email Address" ng-required="true">
                    <p ng-show="userForm.email.$error.required && (userForm.email.$dirty || submitted)" class="help-block">Email is required.</p>
                    <p ng-show="userForm.email.$error.email && (userForm.email.$dirty || submitted)" class="help-block">Enter a valid email.</p>
                </div>

                <!-- PASSWORD -->
                <div class="form-group" ng-class="{ 'has-error' : userForm.password.$invalid && (userForm.password.$dirty || submitted)}">
                    <label>Password</label>
                    <input type="Password" name="password" class="form-control" ng-model="user.passwrd" placeholder="Your Password" ng-required="true">
                    <p ng-show="userForm.password.$error.required && (userForm.password.$dirty || submitted)" class="help-block">Your password is required.</p>
                </div>

                <!-- TERMS & CONDITIONS -->
                <div class="form-group" ng-class="{ 'has-error' : userForm.terms.$invalid && (userForm.terms.$dirty || submitted)}">
                    <label>Accept Terms & Conditions</label>
                    <input type="checkbox" value="" name="terms" ng-model="user.terms" ng-required="true" />
                    <p ng-show="userForm.terms.$error.required && (userForm.terms.$dirty || submitted)" class="help-block">Accept terms & conditions.</p>
                </div>

                <!-- ng-disabled FOR ENABLING AND DISABLING SUBMIT BUTTON -->
                <!--<button type="submit" class="btn btn-primary" ng-disabled="userForm.$invalid">Register</button>-->
                <button type="submit" class="btn btn-primary col-lg-offset-6">Register</button>

            </form>

            <pre>{{user}}

            </pre>
      </div>
     </div>
  </div>
&#13;
&#13;
&#13;

Main.js:

&#13;
&#13;
var app = angular.module('TempleWebApp', [ 'ngRoute']);

app.controller('RegisterCtrl', function ($scope,$location, $http) {

  $scope.user = {};
  $scope.user.name= "" ;
  $scope.user.email ="";
  $scope.user.passwrd="";
  $scope.user.terms="";

    // function to submit the form after all validation has occurred
    $scope.submitForm = function () {

        // Set the 'submitted' flag to true
        $scope.submitted = true;

       $http.post("register.php",{'username':$scope.user.name,'email':$scope.user.email,'password':$scope.user.passwrd})
        .success(function(data,status,headers,config){
            console.log("Inserted Successfully!");
            });

    };
});
&#13;
&#13;
&#13;

PHP代码。

&#13;
&#13;
<?php
$data = json_decode(file_get_contents("php://input"));
$username = $data->username;
$email = $data->email;
$password = $data->password;
$con = mysql_connect("localhost","root","");
mysql_select_db("userregister");
$sql = "insert into user(username,email,password) values($username,'$email','$password')";
$result = mysql_query($sql);

?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

尝试以下列方式使用mysqli(另请注意,您应该创建变量$ dbname并为其分配正确的dbname:

$data = json_decode(file_get_contents("php://input"));
$username = @$data->username;
$email = @$data->email;
$password = @$data->password;
$dbname = '';
$conn = new mysqli("localhost","root","",$dbname);

$check = "SELECT * FROM user WHERE email='$email'";

//The following rows check whether this email already exists in the DB

$results = $conn->query($check);
if($results && mysqli_num_rows($results)>0)
{
    echo "email";
    die;
}

//The following rows will work only if there is no such email in the DB

    if($conn->connect_error)
{
    echo "false";
    die;
}

$sql = "INSERT INTO user VALUES values($username,'$email','$password')";

if ($conn->query($sql) === true)
{
    echo "true";
}

您还需要更改您的Javascript以适应可能的事件:

&#13;
&#13;
       $http.post("register.php",{'username':$scope.user.name,'email':$scope.user.email,'password':$scope.user.passwrd})
		.success(function(data,status,headers,config){
         if(data == 'true'){
           console.log("Inserted Successfully!");
           }
         else if(data == 'email'){
           console.log("The email already exists");
         }
         else{
           console.log("There was an issue connecting to the DB");
         }

         
			
			});
&#13;
&#13;
&#13;