2015-10-23 83 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的,但只是在考驗一些角度。