简体   繁体   中英

how can i insert data into 2 tables via single query

i have two table videos and profile. videos have two columns Id and Name and profile have a column video_id when i save the video name 'name' id will auto incremented i want that when i store video. the incremented id of videos will automatically stored in video_id of profile table

  <?php
  $query="insert into videos(name) values('any_name);
  mysqli_query($link,$query);
  ?>

i want that the id of videos table willl automatically saved to video_id column of profile table. plese help

Chances are you won't be using just one query, as you don't know the Id of the new record you're about to insert. You'll need to perform the insert, and then do a second query getting that record back. You'll probably be best off doing something like

'SELECT Id FROM videos WHERE name = any_name ORDER BY name DESC'

It's in descending order as video name is not necessarily unique, and ordered in descending fashion so your most recently created Id (assumedly the one you want as you will have just created it) is in the first position of the resulting array.

You can then go on to perform an insert into your other table using the data you just retrieved.

This solution isn't guaranteed to work, but if you're in a low traffic environment, it will mostly suit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM