为什么我不能使用angularjs将数据插入mysql数据库

时间:2016-05-18 04:59:50

标签: php mysql angularjs

我正在学习angularjs,所以我尝试制作一个应用程序,以便从mysql数据库中列出电影。表单的html页面显示良好,但是当我输入数据时,它不会进入数据库。

这是我的HTML表单



<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<head>
	<title>Feeder Form</title>
	
</head>
<body>

<div class="container" >

	<h3>Enter Movie Details </h3>
		<form action="" method="" ng-controller="MovieFeederCtrl" enctype="multipart/form-data" id="feeder_form" role="form" class="form-horizontal">

		<fieldset>
			<div class="form-group">
				<label class="control-label col-md-2" >Name:</label>
				<div class="col-md-10">
					<input class="form-control" size="" type="text" id="name" ng-model="name" ></div>
				</div>
			<div class="form-group">
				<label class="control-label col-md-2">Release Date:  </label>
				<div class="col-md-10">
					<input class="form-control" type="date" id="releaseDate" ng-model="releaseDate">
				</div>
			</div>

			
			  
			<div class="form-group">
				<label class="control-label col-md-2" >Genre: </label>
				<div class="col-md-10">
					<input class="form-control" type="text" id="genre" ng-model="genre" placeholder="Genre">
				</div>
			</div>
			 
			<div class="form-group">
				<label class="control-label col-md-2">Cast: </label>
				<div class="col-md-10">
					<textarea class="form-control" id="cast" ng-model="cast" placeholder="Cast"></textarea>
				</div>
			</div>

			<div class="form-group">
				<label class="control-label col-md-2">Parental Guidance:</label>
				<div class="col-md-10">
					<input class="form-control" type="text" id="parentalGuidance" ng-model="parentalGuidance">
				</div>
			</div>
			  
			<div class="form-group">
				<label class="control-label col-md-2">Directors:</label>
				<div class="col-md-10">
					<input class="form-control" type="text" id="directors" ng-model="directors">
				</div>
			</div>
			  
			<div class="form-group">
				<label class="control-label col-md-2">Producers:</label>
				<div class="col-md-10">
					<input class="form-control" size="" type="text" id="producers" ng-model="producers" placeholder="Producers">
				</div>
			</div>
			  
			<div class="form-group">
				<label class="control-label col-md-2">Plot: </label>
				<div class="col-md-10">
					<textarea class="form-control" id="plot" ng-model="plot" placeholder="Plot"></textarea>
				</div>
			</div>
			
			<div class="form-group">
				<label class="control-label col-md-2">Description: </label>
				<div class="col-md-10"> 
					<textarea class="form-control" id="description" ng-model="description" placeholder="Enter description"></textarea>
				</div>
			</div>
			  
			<div class="form-group">
				<label class="control-label col-md-2">Writers: </label>
				<div class="col-md-10">
					<input class="form-control" type="text" id="writers" ng-model="writers" placeholder="Writers">
				</div>
			</div>
		</fieldset>

		<div class="form-group row">
		<label class="control-label col-md-3"><input type="reset" value="Reset"></label>
		<label class="control-label col-md-3"><input type="submit" ng-click="submitmovie()" value="Submit"></label>
		</div>


		</form>
</div>
&#13;
&#13;
&#13;  这是我的控制器代码

&#13;
&#13;
var movieControllers = angular.module('movieControllers',[]);


movieControllers.controller('MovieFeederCtrl',function ($http, $scope) {
	$scope.submitmovie = function() {
		$http.post("submitmovie.php", {'name':$scope.name,'releaseDate':$scope.releaseDate,'coverShot':$scope.coverShot,'genre':$scope.genre,'cast':$scope.cast,'parentalGuidance':$scope.parentalGuidance,'directors':$scope.directors,'producers':$scope.producers,'plot':$scope.plot,'trailerLink':$scope.trailerLink,'description':$scope.description,'writers':$scope.writers}).success(function(data,status,headers,config)
		{
			console.log("Movie submitted successfully")
		}).error(function(data, status,headers,config)
			{
				console.log("An error occured");
			});
	};
});
&#13;
&#13;
&#13;

这是我的submitmovie.php

&#13;
&#13;
<?php
	$data = json_decode(file_get_contents("php://input"));

	$name = mysql_real_escape_string($data->name);
	$releasedate = mysql_real_escape_string($data->releaseDate);
	$covershot = mysql_real_escape_string($data->coverShot);
	$genre = mysql_real_escape_string($data->genre);
	$cast = mysql_real_escape_string($data->cast);
	$parentalguidance = mysql_real_escape_string($data->parentalGuidance);
	$directors = mysql_real_escape_string($data->directors);
	$producers = mysql_real_escape_string($data->producers);
	$plot = mysql_real_escape_string($data->plot);
	$trailerlink = mysql_real_escape_string($data->trailerLink);
	$description = mysql_real_escape_string($data->description);
	$writers = mysql_real_escape_string($data->writers);
	
	mysql_connect('localhost','root','');
	mysql_select_db(cinemapp);

	$insert = "INSERT INTO movies (name, releasedate, genre, cast, pguidance, producers, directors, plot, description, writers) VALUES ('".$name."', '".$releaseDate."', '".$genre."', '".$cast."', '".$parentalguidance."', '".$directors."', '".$producers."', '".$plot."', '".$description."', '".$writers."')";
	$insert_success = query($insert);
	if($insert_success)
	{
		echo "Movie inserted";
	}

?>
&#13;
&#13;
&#13;

我尝试使用if函数检查数据是否到达php页面,但到目前为止它还没有工作。 你能帮我找到问题吗

1 个答案:

答案 0 :(得分:1)

您需要存储mysql_connect()调用的结果,如下所示:

$conn = mysql_connect('localhost','root','');

然后需要在mysql_query()方法中传递这个$ conn对象:

$insert_success = mysql_query($insert, $conn);

您使用的query()方法来自不同的库。即您正在混合来自不同图书馆的代码。

您可以在此处找到更多信息: http://www.tutorialspoint.com/php/mysql_insert_php.htm