dongwan5381 2015-10-23 19:05 采纳率: 100%
浏览 8
已采纳

AngularJS将空白发布到数据库

First of all forgive my bad English.
So when making this code I bumped into an error.

My code:

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>

Angular

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";

When inserting into the database it's just inserting empty rows.
I can't find the error myself but I guess it has something to do with:

$data = json_decode(file_get_contents("php://input"));
  • 写回答

1条回答 默认 最新

  • dtpt75860 2015-10-23 19:36
    关注

    So I kinda found the answer...

    I had to put the connection above the mysql_real_escape_string.

    Result:

    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";
    

    Yes I'm aware I should use PDO or mysqli but was just testing some angular.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘