AngularJS将空白发布到数据库

时间:2015-10-23 19:05:10

标签: php angularjs database

首先原谅我的英语不好。
因此,在制作此代码时,我遇到了错误。

我的代码:

HTML

<div ng-app="todo" ng-controller="listController">
    <div class="item">
        <form>
            <fieldset>
                <legend>Add an item</legend>
                <label for="item-name">Item Name</label>
                <input type="text" name="item-name" ng-model="itemName" id="item-name">

                <label for="item-deadline">Item Deadline</label>
                <input type="text" name="item-deadline" ng-model="itemDeadline" id="item-deadline">

                <label for="item-description">Item Description</label>
                <input type="text" name="item-description" ng-model="itemDescription" id="item-description">

                <label></label>
                <input type="submit" ng-click="itemInsert()">
            </fieldset>
        </form>
    </div>
</div>

var app = angular.module('todo', []);
app.controller('listController', function ($scope, $http) {
    $scope.itemInsert = function () {
        $http.post("pages/insert.php", {'itemName': $scope.itemName, 'itemDeadline': $scope.itemDeadline, 'itemDescription': $scope.itemDescription})
        .success(function(data, status, headers, config){
            console.log('Success');
        });
    };
});

insert.php

$data = json_decode(file_get_contents("php://input"));
$itemName = mysql_real_escape_string($data->itemName);
$itemDeadline = mysql_real_escape_string($data->itemDeadline);
$itemDescription = mysql_real_escape_string($data->itemDescription);

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("todo") or die(mysql_error());
mysql_query("INSERT INTO `items`(`name`, `deadline`, `description`) VALUES ('" . $itemName . "', '" . $itemDeadline . "', '" . $itemDescription . "')");
Print "Success";

当插入数据库时​​,它只是插入空行。
我自己无法找到错误,但我想这与错误有关:

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

1 个答案:

答案 0 :(得分:0)

所以我找到了答案......

我必须把连接放在mysql_real_escape_string上面。

结果:

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("todo") or die(mysql_error());

$data = json_decode(file_get_contents("php://input"));
$itemName = mysql_real_escape_string($data->itemName);
$itemDeadline = mysql_real_escape_string($data->itemDeadline);
$itemDescription = mysql_real_escape_string($data->itemDescription);

mysql_query("INSERT INTO `items`(`name`, `deadline`, `description`) VALUES ('" . $itemName . "', '" . $itemDeadline . "', '" . $itemDescription . "')");
Print "Success";

是的我知道我应该使用PDO或mysqli但只是测试一些角度。