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

时间:2022-09-25 20:02:55

I am learning angularjs, so I tried to make a app to list movies out of a mysql database. The html page for the form displays well but when I enter data it does not go into the database.

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

Here's my HTML form

这是我的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>
Here is my controllers code

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

And here is my submitmovie.php

这是我的submitmovie.php

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

?>

I tried to use the if function to check if the data gets to the php page but so far it hasn't worked. Would you please help me find the problem

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

1 个解决方案

#1


1  

You need to store the result of the mysql_connect() call like this:

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

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

This $conn object then needs to be passed like this in the mysql_query() method:

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

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

The query() method you used is from a different library. i.e. You are mixing codes from different libraries.

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

You can find more info here: http://www.tutorialspoint.com/php/mysql_insert_php.htm

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

#1


1  

You need to store the result of the mysql_connect() call like this:

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

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

This $conn object then needs to be passed like this in the mysql_query() method:

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

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

The query() method you used is from a different library. i.e. You are mixing codes from different libraries.

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

You can find more info here: http://www.tutorialspoint.com/php/mysql_insert_php.htm

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