1

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.

Here's my HTML form

<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

<?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

2
  • Does it hit the server ? Commented May 18, 2016 at 5:01
  • @AnikIslamAbhi What do you mean by hitting the server? Commented May 18, 2016 at 12:54

1 Answer 1

1

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

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

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

$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.

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

Sign up to request clarification or add additional context in comments.

1 Comment

I think the syntax you have given me is not correct because the last arguement in mysql_connect() is supposed to be the password to the database not the name of the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.