1

我有一个表格,我需要在一个表格中插入一些信息。然后从该插入中获取自动增量 id 并在下一个插入语句中使用它。除了抓取最后一个 id 之外,我得到了一切工作。mysql_insert_id()?

include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
$query = "INSERT into `".$db_table."` (title_id,seg_ordering,seg_name,seg_description,seg_duration) VALUES ('" . $_POST['title_id'] . "','" . $_POST['seg_ordering'] . "','" . $_POST['seg_name'] . "','" . $_POST['seg_description'] . "','" . $_POST['seg_duration'] . "')";
mysql_query($query);
mysql_close($link);





include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);


function select_max_id($id="id",$my_table="jos_mad_segments")
{


  $SQL = "SELECT MAX($seg_id) FROM ".$my_table;
  $db->query($SQL);
  if ($db->next_record())
  {
     $max_id = $db->f(0);
  }
$db->close();
return $max_id;
} 



//data insert 2 //

include("config2.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
$query = "INSERT into `".$db_table."` (seg_id,file_video_UNC,file_video_URL) VALUES ('" . '$max_id' . "','" . $_POST['file_video_UNC'] . "','" . $_POST['file_video_URL'] . "')";
mysql_query($query);
mysql_close($link);
4

1 回答 1

0

如果 Auto Id 是整数类型,您可以在 table 上触发 max(auto_id) 函数以从 table 中获取最后一个 auto id///

于 2010-12-17T15:10:44.237 回答