我正在尝试从 php 中的数据库插入获取自动增量 ID 回我的 javascript ajax 调用:
我的 ajax 调用如下所示:
//get values
var note = $('#note1').val();
alert(userID);
alert(beerID);
var ajaxSettings = {
type: "POST",
url: "atn.php",
data: {u:userID , b:beerID ,n:note},
success: function(data){
alert(data);
} ,
error: function(xhr, status, error) { alert("error: " + error); }
};
$.ajax(ajaxSettings);
return false;
我的 php 脚本如下所示:
<?php
error_log("starting code");
require_once('myConnectDB.inc.php');
$u = $_POST['u'];
$b = $_POST['b'];
$n = $_POST['n'];
//do some checks etc
$db = new myConnectDB();
$u = $db->real_escape_string($u);
$n = $db->real_escape_string($n);
$b = $db->real_escape_string($b);
$query3 = "INSERT INTO tn (userID,beerID,note) VALUES ($u, '$b', '$n')";
$result = $db->query($query3);
$dbID = mysql_insert_id();
echo $dbID;
?>
在 php 脚本运行后,我试图发回的 $dbID 没有收到警报。我在警报中收到此错误:
<br />
<b>Warning</b>: mysql_insert_id() [<a href='function.mysql-insert-id'>function.mysql-insert-id</a>]: A link to the server could not be established in <b>/home4/m133414/public_html/atn.php</b> on line <b>23</b><br />