开发者

Using a single form inserting data into two differen tables though needing to insert last auto id from the first insert into the next

开发者 https://www.devze.com 2023-01-31 07:02 出处:网络
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 th

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消