简体   繁体   English

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

[英]How to Get last inserted record from database using angularjs and php?

I have created two pages. 我创建了两个页面。 One page is BookTicket which contains form. 一页是BookTicket,其中包含表单。 It has two buttons. 它有两个按钮。 one buttons submits form and stores value in database. 一个按钮提交表单并将值存储在数据库中。 other button will navigate to payment page. 其他按钮将导航到付款页面。 other page is payment. 其他页面是付款。 in BookTicket page the form values is submitted and stored in database. 在BookTicket页面中,表单值被提交并存储在数据库中。 It is working correctly. 它工作正常。 I want to Display the currently entered data in form which is in Bookticket into Payment page so that user can see the details entered in bookticket page. 我想将当前输入的数据显示在Bookticket的付款页面中,以便用户可以看到在bookticket页面中输入的详细信息。 How To achieve This? 如何实现这一目标? How to get Currently inserted record from database and display it in payment page? 如何从数据库中获取当前插入的记录并将其显示在付款页面中?

What can I write in payment.html page and php page for payment to get presently entered record? 我可以在payment.html页面和php页面上写什么来获得当前输入的记录?

Bookticket.html 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> 

main.js main.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 }); 

BookTicket.php BookTicket.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); } ?> 

you can get last inserted record id by using mysql_insert_id() 您可以使用mysql_insert_id()获取最后插入的记录ID

example: 例:

    $last_insert = mysql_insert_id();

check http://php.net/manual/en/function.mysql-insert-id.php for more details 有关详细信息,请查看http://php.net/manual/en/function.mysql-insert-id.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM