gpt4 book ai didi

php - 为什么我不能使用angularjs向mysql数据库插入数据

转载 作者:行者123 更新时间:2023-11-28 23:31:12 24 4
gpt4 key购买 nike

我正在学习 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>
这是我的 Controller 代码

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

这是我的 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";
}

?>

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

最佳答案

您需要像这样存储 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

关于php - 为什么我不能使用angularjs向mysql数据库插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com