2015-10-23 133 views
0

首先原谅我的坏英语。
所以,当我编写这段代码时,我遇到了一个错误。AngularJS发布空数据库

我的代码:

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")); 
+0

表单数据是否存在于网络请求中? – hughes

+0

@hughes这就是它显示在网络http://prntscr.com/8ujjgq – Edsardio

+0

我看到你找到你自己的答案,但我建议你检查表单数据中的请求,以'insert.php'。如果您在网络标签中点击该请求,则会看到一个“请求正文”部分。如果您看到预期数据,则可以将问题缩小到客户端(如果数据不存在)或服务器端(如果数据存在) – hughes

回答

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的,但只是在考验一些角度。