php - AngularJS 与 PHP : why my app can't save strings sent by input text?

标签 php html mysql json angularjs

我有一个 AngularJS 和 PHP 应用程序,可以在 mysql 数据库中保存和搜索。

在我的 index.html 中,我有一个显示数据库中所有记录的表格,一个包含两个输入文本的表单和一个保存信息的按钮。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<title>Insert title here</title>
</head>
<body ng-app="carApp" ng-controller="carCtrl">
    <div class="container">
        <div class="jumbotron">
            <p>Simple CRUD in AngularJS and PHP</p>
        </div>
        <table class="table">
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Plate</th>
            </tr>
            <tr ng-repeat="c in cars">
                <td>{{c.car_id}}</td>
                <td>{{c.car_name}}</td>
                <td>{{c.car_plate}}</td>
            </tr>
        </table>
        <br /> <br />

        <h3>New Car</h3>
        <form role="form">
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label>Car Name: {{name}}</label> <input type="text" ng-model="name" class="form-control">
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label>Car Plate: {{plate}}</label> <input type="text" ng-model="plate" class="form-control">
                    </div>
                </div>
            </div>
            <button type="button" ng-click="save()" class="btn btn-primary">Save</button>
        </form>
    </div>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script>
        var app = angular.module('carApp', []);
        app.controller('carCtrl', function($scope, $http) {
            $scope.cars = [];
            $scope.name = '';
            $scope.plate = '';

            getCars();

            $scope.save = function() {
                var car = {
                    name : $scope.name,
                    plate : $scope.plate
                };

                $http.post("../database/insert/newCar.php", car).success(
                        function() {
                            console.log(car);
                            getCars();
                        });
            };

            function getCars() {
                $http.get("../database/select/cars.php").success(
                        function(response) {
                            $scope.cars = response;
                        });
            }
            ;
        });
    </script>
</body>
</html>

数据库中的搜索没问题,但是当我尝试插入时,应用程序仅在两个字段都填充了 numbers 时才会保存。当我尝试保存 strings 时,什么也没有保存。我不知道为什么会这样,因为我在两个字段中都使用了输入类型 text 并且数据库中的列是 varchar

EG:
Car Name: 111111111, Car Plate: 1111111111
Result: Information Saved.

Car Name: 333333333, Car Plate: 12345667
Result: Information Saved

Car Name: 333333333, Car Plate: 12 34 56 67
Result: Information not Saved

Car Name: 333 33 33 33, Car Plate: 12345667
Result: Information not Saved

Car Name: aaaaaaaaaa, Car Plate: bbbbbbbbb
Result: Information not Saved

Car Name: hj3k4hjk34h, Car Plate: 1a2b3ce49
Result: Information not Saved

这里是 newCar.php:

<?php
    include '../connector/mysql_connection.inc';

    $json = file_get_contents('php://input');

    $car = json_decode($json);

    $sql = "INSERT INTO `testing_angular`.`car` (`car_name`, `car_plate`) VALUES (" . $car->name . ", " . $car->plate . ");"; 

    $connection->query($sql);
    $connection->close();
?>

这是“汽车”表:

CREATE TABLE IF NOT EXISTS `car`(
    `car_id` int(11) NOT NULL AUTO_INCREMENT,
    `car_name` varchar(255) NOT NULL,
    `car_plate` varchar(50) NOT NULL,
    PRIMARY KEY (`car_id`),
    UNIQUE KEY `car_plate` (`car_plate`)
);

最佳答案

我认为如果您像这样引用您的插入值会更好:

"INSERT INTO `testing_angular`.`car` (`car_name`, `car_plate`) 
    VALUES ('" . $car->name . "', '" . $car->plate . "');";

当然,以这种方式构造语句时,通常会出现 SQL 注入(inject)问题。

关于php - AngularJS 与 PHP : why my app can't save strings sent by input text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29544640/

相关文章:

mysql - 在执行 SQL 查询时需要帮助

MYSQL如何计算日期范围内的列的总和

PHP strtotime() 与 date.js parse()

php - 在 Gravity Forms 目录插件中添加下载 Pdf 的选项

javascript - 如何对齐 <section> 标签中的列表?

php - 在 foreach() 函数内部,DIV 相互重叠

java - 法语字符编码问题 è 在 java 中显示为 è

php - 使用 jquery 和 php 检索 mysql 数据

php - 如何在 PHP 中将 "first day of the week"设置为星期四

java - 通过 JDBC 更新的散列密码已损坏。 (更多的是编码问题而不是散列)