如何获取插入表中的行的自动生成的 id? [英] How to get the auto-generated ids of the row which is inserted into table?

查看:44
本文介绍了如何获取插入表中的行的自动生成的 id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计酒店预订系统,在插入客户信息后,我必须发回包含在插入客户信息时自动生成的预订 ID 的确认页面.

I am designing Hotel Booking system, where after insertion of the customer information I have to send back conformation page with the booking_id which is auto-generated when customer information is inserted.

我的问题是如何在相同的php代码中插入数据后获取booking id

还请注意,一个人正在插入数据,所以我认为仅返回最后插入的行无法实现请帮我解决这个问题.

please Note also more that one person is Inserting the data so I think It can't be achieve by just returning last inserted row Please help me on this.

我的 javascript 是:

My javascript is:

<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>

我的服务代码book.php

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

推荐答案

U will get it with mysql_insert_id() 插入数据库后.它会自动返回最新的自动增量 ID.更好的一个 - 使用 PDO 或 MySQLI,因为 mysql_insert_id() 已被弃用.

U will get it with mysql_insert_id() after you insert it into the database. it will automatically give you back the latest Auto Increment ID. better one - use PDO or MySQLI because mysql_insert_id() is deprecated.

这篇关于如何获取插入表中的行的自动生成的 id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆