php - 如何使用 angularjs 和 php 从数据库中获取最后插入的记录?

标签 php mysql angularjs

我创建了两个页面。 一页是包含表格的 BookTicket。它有两个按钮。一个按钮提交表单并将值存储在数据库中。 其他按钮将导航到付款页面。 另一页是付款。 在 BookTicket 页面中,表单值被提交并存储在数据库中。它工作正常。 我想将 Bookticket 中的表单中当前输入的数据显示到付款页面,以便用户可以看到在 bookticket 页面中输入的详细信息。 如何实现这一点? 如何从数据库中获取当前插入的记录并显示在支付页面?

支付的payment.html页面和php页面写什么可以得到当前录入的记录?

Bookticket.html

  
<div class="col-lg-12 well">

  <h2 style="text-align:center;">BOOK MOVIE TICKETS</h2>
     
	 <p style="text-align:center;">  NOTE : 1. FIRST FILL THE DETAILS AND REGISTER. <br> 2. AFTER FILLING FORM GO TO PAYMENT PAGE BY CLICKING ON PROCEED TO PAY  </p>

<div class="col-lg-6 well" style="margin-top:1em;" >

     <video width="430" height="340" controls>
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>
	  
</div>


  
           
<div class="col-lg-6 " style="margin-top:1em;">
<form  name="bookticket" ng-app="BookTicket" ng-controller="BookTicketCtrl" ng-submit="submitForm(bookticket.$valid)" novalidate>	      
				
		  <div class="form-group col-lg-6" ng-class="{ 'has-error' : BookTicket.city.$invalid && (BookTicket.city.$dirty || submitted)}">
                    <label>Select City</label>
                    <select name="city" class="form-control"
                            ng-model="city"
                            ng-options="city.Name as city.Name for city in cityList"
                            ng-required="true" >
							 
					  <option value="">Select </option> 
                   </select>
				   <span class="help-block" ng-show="BookTicket.city.$invalid && BookTicket.city.$error.required && (BookTicket.city.$dirty || submitted)" >City Name is required.</span> 
         </div>
		
		<div class="form-group col-lg-6" ng-class="{ 'has-error' : BookTicket.locations.$invalid && (BookTicket.locations.$dirty || submitted)}">
                    <label>Select Location</label>
                    <select name="location" class="form-control"
                            ng-model="locations"
                            ng-options="location.Name as location.Name for location in locationList"
                            ng-required="true">
						 <option value="">Select </option>  
                   </select>
				 <span ng-show="BookTicket.locations.$invalid && BookTicket.locations.$error.required && (BookTicket.locations.$dirty || submitted)" class="help-block">Location Name is required.</span>  
         </div>
		 
		 <div class="form-group col-lg-12" ng-class="{ 'has-error' : BookTicket.theater.$invalid && (BookTicket.theater.$dirty || submitted)}">
                    <label>Select Theater </label>
                    <select name="theater" class="form-control"
                            ng-model="theater"
                            ng-options="theater.Name as theater.Name for theater in theaterList"
                            ng-required="true">
						 <option value="">Select </option> 
                   </select>
				  <span ng-show="BookTicket.theater.$invalid && BookTicket.theater.$error.required && (BookTicket.theater.$dirty || submitted)" class="help-block">Theater Name is required.</span> 
         </div>
		
         <div class="form-group col-lg-6" ng-class="{ 'has-error' : BookTicket.showdate.$invalid && (BookTicket.showdate.$dirty || submitted)}">
                    <label>Select Show Date </label>
                    <select name="showdate" class="form-control"
                            ng-model="showdate"
                            ng-options="showdate.dates as showdate.dates for showdate in showdateList"
                            ng-required="true">
						 <option value="">Select </option> 	 
                   </select>
				   <span ng-show="BookTicket.showdate.$invalid && BookTicket.showdate.$error.required && (BookTicket.showdate.$dirty || submitted)" class="help-block">Show Date is required.</span>
         </div>
		
		 <div class="form-group col-lg-6" ng-class="{ 'has-error' : BookTicket.showtime.$invalid && (BookTicket.showtime.$dirty || submitted)}">
                    <label>Select Show Time </label>
                    <select name="showtime" class="form-control"
                            ng-model="showtime"
                            ng-options="showtime.times as showtime.times for showtime in showtimeList"
                           ng-required="true">
					   <option value="">Select </option> 		 
                   </select>
				<span ng-show="BookTicket.showtime.$invalid && BookTicket.showtime.$error.required && (BookTicket.showtime.$dirty || submitted)" class="help-block">Show Time is required.</span>
         </div>
		 
		  <div class="form-group col-lg-6" ng-class="{ 'has-error' : BookTicket.classes.$invalid && (BookTicket.classes.$dirty || submitted)}">
                    <label>Select Class </label>
                    <select name="classes" class="form-control"
                            ng-model="classes"
                            ng-options="class.Name as class.Name for class in classList"
                           ng-required="true">
						 <option value="">Select </option> 	 
                   </select>
				 <span ng-show=" BookTicket.classes.$invalid && BookTicket.classes.$error.required && (BookTicket.classes.$dirty || submitted)" class="help-block">Class is required.</span> 
         </div>
		
		<div class="form-group col-lg-6" ng-class="{ 'has-error' : BookTicket.seatno.$invalid && (BookTicket.seatno.$dirty || submitted)}">
                    <label>Select Seat Number </label>
                    <input class="form-control" type="number" name="seatno"  ng-model="seatno" min="1" max="500" ng-required="true">
					 <span ng-show="BookTicket.seatno.$invalid && BookTicket.seatno.$error.required && (BookTicket.seatno.$dirty || submitted)" class="help-block">Seat Number is required.</span> 

         </div>
			 
		
		 <div class="col-lg-12 well " ng-repeat="error in errors" style="background-color:red; margin-bottom:1.5em;"> {{ error}}  </div>
         <div class="col-lg-12 well" ng-repeat="msg in msgs" style="background-color:green; margin-bottom:1.5em;"> {{ msg}}     </div>	
                    
           <button type="submit" class="btn btn-success col-lg-8 col-lg-offset-2" >REGISTER DETAILS</button>
		   
		  <button type="submit" class="btn btn-success col-lg-8 col-lg-offset-2" style="margin-top:1.5em;" ui-sref="payment" >PROCEED TO PAY</button>

</form>
</div>


</div>

主要.js

// create angular controller for BookTicket
app.controller('BookTicketCtrl', function ($scope,$location, $http) {
  
  
  
        
  
  
  
 
    $scope.cityList = [
                         { Name: 'BANGLORE' },
                         { Name: 'TUMKUR' },
	                     { Name: 'MYSORE' },
	                     { Name: 'MANGLORE' },
	                     { Name: 'HASAN' }
                      ];
 
    $scope.locationList = [
                         { Name: 'RAJAJINAGAR STREET' },
                         { Name: 'TUMKUR STRRET' },
	                     { Name: 'MYSORE STREET' },
	                     { Name: 'MANGLORE STREET' },
	                     { Name: 'HASAN STRRET' }
                      ];
					  
  $scope.theaterList = [
                         { Name: 'ABC THEATER (BANGLORE,RAJAJINAGAR)' },
                         { Name: 'HOME THEATER (TUMKUR,TUMKUR STREET)' },
	                     { Name: 'MYSORE THEATER (MYSORE ROAD, MYSORE)' },
	                     { Name: 'MANGLORE THEATER (MANGLORE ROAD, MANGLORE)' },
	                     { Name: 'HASAN THEATER (HASAN ROAD, HASAN)' }
                      ];
 

  $scope.showdateList = [
                         { dates: '1-MAR-2017' },
                         { dates: '10-MAR-2017' },
	                     { dates: '13-MAR-2017' },
	                     { dates: '15-MAR-2017' },
	                     { dates: '20-MAR-2017' }
                      ];
 $scope.showtimeList = [
                         { times: '10 AM' },
                         { times: '12 AM' },
	                     { times: '3 PM' },
	                     { times: '5 PM' }

                      ];
					  
 $scope.classList = [
                         { Name: 'A' },
                         { Name: 'B' },
	                     { Name: 'C' },
	                     { Name: 'D' },
	                     { Name: 'E' }
                      ];
					  
  /*$scope.seatList = [
                         { number: 'A' },
                         { number: 'B' },
	                     { number: 'C' },
	                     { number: 'D' },
	                     { number: 'E' }
                      ]; */
 
 
                $scope.errors = [];
                $scope.msgs = [];


    // function to submit the form after all validation has occurred			
    $scope.submitForm = function (isValid) {
		
		

		
		
		       // Set the 'submitted' flag to true
                  $scope.submitted = true;
		
                   $scope.errors.splice(0, $scope.errors.length); // remove all error messages
                    $scope.msgs.splice(0, $scope.msgs.length);

					
		 if (isValid) { 


              
                  
		 
					
                    $http.post('bookticket.php', 
					             {'city':        $scope.city, 
								  'locations':   $scope.locations, 
								  'theater':     $scope.theater,
								  'showdate' :   $scope.showdate ,
								  'showtime'  :  $scope.showtime   ,
								  'classes' :    $scope.classes  ,
								  'seatno'  :    $scope.seatno  
								  } 
							   )
					
					
					.success(function(data, status, headers, config) {
                            if (data.msg != '')
                        {
                            $scope.msgs.push(data.msg);
                        }
                        else
                        {
                            $scope.errors.push(data.error);
                        }
                      })
					
					.error(function(data, status) { // called asynchronously if an error occurs or server returns response with an error status.
                        $scope.errors.push(status);
                    });
					
					//$location.path('/payment');
	
			 } // closing bracket for isvalid 
					
					else{ alert ('All Fields Are Required... please fill all the feilds..')   }
	
	     };// closing bracket for $scope.submitForm = function 

	
});

书票.php

<?php

$data = json_decode(file_get_contents("php://input"));

$city = mysql_real_escape_string($data->city);
$locations = mysql_real_escape_string($data->locations);
$theater = mysql_real_escape_string($data->theater);
$showdate = mysql_real_escape_string($data->showdate);
$showtime = mysql_real_escape_string($data->showtime);
$classes = mysql_real_escape_string($data->classes);
$seatno = mysql_real_escape_string($data->seatno);


$con = mysql_connect('localhost', 'root', '');
mysql_select_db('bookticket', $con);

$qry_em = 'select count(*) as cnt from users where seatno ="' . $seatno . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);

if($res['cnt']==0){
$qry = 'INSERT INTO users (city,locations,theater,showdate,showtime,classes,seatno) values 
("' . $city . '","' . $locations . '","' . $theater . '","' . $showdate . '","' . $showtime . '","' . $classes . '","' . $seatno . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
    $arr = array('msg' => "REGISTERED DETAILS SUCCESSFULLY!!!! NOW CLICK ON CONTINUE TO PAY TO COMPLETE PAYMENT", 'error' => '');
    $jsn = json_encode($arr);
    print_r($jsn);
} else {
    $arr = array('msg' => "", 'error' => 'ERROR OCCURED!!!! PLEASE TRY AGAIN...');
    $jsn = json_encode($arr);
    print_r($jsn);
}
}

else
{
     $arr = array('msg' => "", 'error' => 'Seat no is  Already Taken');
    $jsn = json_encode($arr);
    print_r($jsn);
}

?>

最佳答案

您可以使用 mysql_insert_id() 获取最后插入的记录 id

例子:

    $last_insert = mysql_insert_id();

检查 http://php.net/manual/en/function.mysql-insert-id.php了解更多详情

关于php - 如何使用 angularjs 和 php 从数据库中获取最后插入的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42318478/

相关文章:

PHP Google Drive API - 文件上传仅作为 "Untitled"- 文件或只是没有内容

php - 如果 PDO 和 mysqli 不可用,使用 mysql_* 函数是否安全?

javascript - 带有预先输入功能的 Angular Material 自动完成

php - 将产品详细信息插入 'product' 表,并将产品图像插入 'image' 表

iOS UIWebView 和 Cookies 存储

javascript - 检查对象是否是 AngularJS 中的 promise

javascript - 'try' 'catch' 在此 JavaScript 代码中不起作用

php - 如何从某个数字/偏移量开始 AUTOINCREMENT?

php - 使用 imagick PHP 在 png 图像周围添加边框

php - 什么是检测 PHP 消息中垃圾邮件的最佳方法