如何使用json格式返回mysql中插入的行? [英] How to return inserted row in mysql using json format?
问题描述
我必须返回插入的行,该行仅以相同的php代码插入json格式.在这一行中,booking_id是自动生成的密钥,以及其他信息,例如customer_Name,customer_Email,customer_id. 那我如何返回带有附加列booking_id(自动生成)的插入行?.
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)?.
请注意,该问题与上一问题相同,但我忘了问返回行,但只问返回ID.对此表示抱歉. 在此处检查
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>
$(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>
服务器代码
<?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";
}
?>
推荐答案
使用:mysql_insert_id()
或mysqli_insert_id()
返回预订ID.
use: mysql_insert_id()
or mysqli_insert_id()
to return the booking ID.
注意:使用mysql_insert_id
时要小心,因为它在php 5.5中已弃用,以后会被删除
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);
然后在ajax success
函数中,使用php文件而不是html,并在url query string
中传递此预订,并在您的booking.php中使用此预订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+'";
}
然后在bookingConfirmation.php
文件中使用GET
方法来获取此变量:
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
这篇关于如何使用json格式返回mysql中插入的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!