如何在Jquery成功方法中获得“最后插入的数据库”?

好吧,在我的添加联系表单中,我使用Jquery和Php将数据插入到Mysql数据库中。 它已成功将数据插入db。 现在我要将成功的页面重定向到index.php?cdid = $ last_id 。 但是如何在jquery成功方法中获得$ last_id呢? 我只能显示成功的消息。 任何帮助?

Jquery代码:

 $('#addcontact').submit(function(event) { event.preventDefault(); $.ajax({ type: 'POST', url: 'add_contact_process.php', data: $(this).serialize(), dataType: 'json', success: function (data) { $('#success').html(''); $.each( data, function( key, value ) { if(key !== 'error') { $('#success').append('

'+value+'

'); } }); if( ! data.error) { $('#hide').hide(); setTimeout(function () { $('input[type=submit]').attr('disabled', false); window.location.href = "../index.php"; //window.location.href = "../index.php?redcdid="; // need something like above }, 5000); } } }); });

PHP代码:

 <?php ob_start(); @session_start(); require_once("../config.php"); $company_name = ucfirst($_POST['company_name']); $family_name = ucfirst($_POST['family_name']); $msg = array(); $msg['error'] = false; if(empty($company_name)){ $msg[] = "Company name required. "; $msg['error'] = true; } if(empty($family_name)){ $msg[] = "Family name required. "; $msg['error'] = true; } if($msg['error'] === false){ $query_2 = "INSERT INTO contact_details (cdid, family_name, given_name, work_phone, mobile_phone, email, email_private, user_id, created_date, cid) VALUES('', '$family_name', '$given_name', '$work_phone', '$mobile_phone', '$email', '$email_private', '$user_id', '$date', '$cid')"; $query_2 = mysql_query($query_2); $last_id = mysql_insert_id(); if($query_2){ $msg[] = 'Successfully added a new contact. '; $another = "close"; } else{ $msg[] = 'Sorry we can not add a new contact details. '; $msg[] .= mysql_error(); $another = "close"; } } echo json_encode($msg); ?> 

更新:

我正在使用以下代码到Php apge:

 if($query_2){ $msg[] = 'Successfully added a new contact. '; $msg['last_id'] = $last_id; } 

我正在使用以下jquery代码:

 if( ! data.error) { $('#hide').hide(); setTimeout(function () { $('input[type=submit]').attr('disabled', false); var last_id = data.last_id; window.location.href = "../index.php?redcdid=last_id"; }, 5000); } 

然后在响应上传递一个嵌套数组:

 $data['msg'] = $msg; $data['last_insert_id'] = $last_id; echo json_encode($data); 

然后在JS中,你需要将它们调整为:

 success: function (response) { // ^ now this will hold both the messages and the last insert id var data = response.msg; // separate them, messages does in data var last_id = response.last_insert_id; // last_id has the last insert id }, 

你可以得到最后一个插入ID

 mysql_insert_id().... 

bt而不是这个,你应该使用

pdo ……这很简单也很好……

http://php.net/manual/en/book.pdo.php

在pdo你可以通过..执行你的查询

在配置文件中创建pdo实例..

 getMessage(); } ?> 

你可以通过…获取数据

 $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour'); $sth->bindParam(':calories', $calories); $sth->bindParam(':colour', $colour); $sth->execute(); you can get last insert id by $dbh->lastInsertId(); 

pdo简单安全…..

通过使用mysql_insert_id()您将能够获得最后一个ID。

 $last_id = mysql_insert_id(); 

然后在您的PHP代码中,在成功消息后立即执行以下操作。

 $data['msg'] = 'your message'; $data['last_id'] = $last_id; echo json_encode($data); 

然后执行以下操作以获取ajax的响应

 success: function (output) { var data = output.msg; // this is message var last_id = output.last_id; // this is the id }, 

将您的javascript更改为以下内容: