使用单个表单将数据插入两个不同的表,但需要将第一个插入中的最后一个自动ID插入到下一个表中
我有一个表格,需要将一些信息插入到一个表中。然后从该插入中获取自动增量 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);
I have a form I need to insert some information into one table. Then grab the auto increment id from that insert and use it in the next insert statement. I get it all to work except for the grabbing the last 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 Auto Id 是整数类型,您可以在表上触发 max(auto_id) 函数以获取表中最后一个自动 id ///
You can fire max(auto_id) function on the table to get the last auto id from the table if Auto Id is integer type///