I have to return the inserted row which is just inserted into json format in the same php code. In this row booking_id is auto generated key, and other information like customer_Name, customer_Email, customer_id. So How I will return inserted row with additional column booking_id (auto-generated)?.
please Note this question is same as but I forget to asked returning row but asked only returning id. sorry for that. check here
script
<script>
$(document).ready(function(){
$("#register-form").validate({
rules: {
userName: "required",
email: {
required: true,
email: true
},
userContactNumber: "required"
},
messages: {
userName: "Please enter your Name",
userContactNumber: "Please enter your Mobile number",
email: "Please enter a valid email address",
},
submitHandler: function(form) {
var uName = $('#userName').val();
var mailId = $('#email').val();
var mobNum = $('#userContactNumber').val();
$.ajax({
url:"http://localhost/bookRoom/book.php",
type:"POST",
dataType:"json",
data:{type:"booking", Name:uName, Email:mailId, Mob_Num:mobNum},
ContentType:"application/json",
success: function(response){
window.location.href = 'BookingConformation.html';
},
error: function(err){
window.location.href = 'error.html';
}
});
return false; // block regular submit
}
});
});
</script>
server code
<?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","2190");
mysql_select_db("hotels");
if(isset($_POST['type']))
{
if($_POST['type']=="booking"){
$name = $_POST ['Name'];
$mobile = $_POST ['Mob_Num'];
$mail = $_POST ['Email'];
$query1 = "insert into customer(userName, userContactNumber, email) values('$name','$mobile','$mail')";
$query2 = "insert into booking(cust_name, cust_email, cust_mobile) values('$name', '$mail','$mobile')";
$result1=mysql_query($query1);
$result2=mysql_query($query2);
echo json_encode($result1);
}
}
else{
echo "Invalid format";
}
?>
You can access the id that was auto generated in the insert operation with the mysql_insert_id() function
To return it, just create an array with the data you used in the insert and the retrieved id. You can then return the array using json_enode.
use it. i think this is helpful for you
$id = mysql_insert_id();
use: mysql_insert_id()
or mysqli_insert_id()
to return the booking ID.
Note: while using mysql_insert_id
be careful as it's depreciated in php 5.5 and will be removed in future
$id = mysql_insert_id();
echo json_encode($id);
Then in your ajax success
function use php file instead of html and pass this booking in a url query string
and use this booking ID in your booking.php and write a query to fetch that row :
success: function(response){
window.location.href = "BookingConformation.php?bookingID='+response+'";
}
Then in your bookingConfirmation.php
file use GET
method to fetch this variable:
$bookingID=$_GET['bookingID'];
$query1= "select * from bookin where b_id =".$bookingID;
$result = mysql_query($query1);
var_dump($result); //gives your array of insterd row
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.