2013-03-23 231 views
-1

我期待從單一表單中將數據發佈到兩個數據庫表中。 我的數據庫被安排爲如下:將表單數據插入到兩個數據庫表中

數據庫1 - '監視列表'

  • watchlist_id
  • USER_ID
  • 名稱
  • 描述
  • 類別

數據庫2 - 'watchlist_films'

  • watchlist_id
  • film_id

我目前的MySQL查詢看起來是這樣的:$query = "INSERT INTO watchlist_films (watchlist_id, film_id) VALUES ('$watchlist_name['watchlist_id']', '$rt_id') WHERE watchlists ('watchlist_id') = " . $watchlist_name['watchlist_id'];,但我不知道是否有一定是某種形式的INNER JOIN某處?

不知道還有哪些其他信息/代碼需要提供,所以如果這裏沒有太多的細節,我很抱歉,但是,如果還有其他需要的東西,請給我發一條評論,我會提出其他所需的東西。我是一個相對的PHP新手,如果這看起來像一個非常簡單的問題,非常抱歉!

更新基於意見

現在我已經得到了我一半的查詢工作,並已更新了邏輯,以反映它。新的查詢,基本上是做了以下內容:

  • INSERT新的監視列表中'watchlists'
  • 新的監視列表中的SELECT watchlist_id'watchlists'WHERE watchlist_name = $watchlist_name(剛剛創造了新的監視列表中的名稱)和user_id = $user_id
  • INSERT watchlist_id(從以前的選擇查詢)AND film_id納入'watchlist_films'

根據您的通訊經濟需求,我的查詢現在看起來像這樣:

if ($submit == 'Submit') { 
     require_once("db_connect.php"); 

     $watchlist_name = clean_string($_POST['watchlist-name']); 
     $watchlist_description = clean_string($_POST['watchlist-description']); 
     $watchlist_category = $_POST['watchlist-category']; 

     $addWatchlist_bad_message = ''; 
     $addWatchlist_good_message = ''; 

     if ($db_server) { 
      if (!empty($watchlist_name)) { 
       $watchlist_name = clean_string($watchlist_name); 
       $watchlist_description = clean_string($watchlist_description); 
       mysql_select_db($db_database); 

       // Insert new Watchlist into Watchlist index 
       $insert_new_watchlist = "INSERT INTO watchlists (user_id, name, description, category) VALUES ('$user_id', '$watchlist_name', '$watchlist_description', '$watchlist_category')"; 
       mysql_query($insert_new_watchlist) or die("Insert failed. " . mysql_error() . "<br />" . $insert_new_watchlist); 

       // Select new Watchlist ID 
       $select_new_watchlist = "SELECT watchlist_id FROM watchlists WHERE name = " . $watchlist_name; 
       $new_watchlist_id = mysql_query($select_new_watchlist) or die("Insert failed. " . mysql_error() . "<br />" . $select_new_watchlist); 

       // Add film to new Watchlist 
       $add_new_film = "INSERT INTO watchlist_films (watchlist_id, film_id) VALUES ('$new_watchlist_id', '$rt_id')"; 
       mysql_query($add_new_film) or die("Insert failed. " . mysql_error() . "<br />" . $add_new_film); 
       $addWatchlist_good_message = '<div class="alert alert-success">Watchlist created successfully!</div>';?> 
       <script> 
        $('a.add-watchlist').trigger('click'); 
       </script><?php 
      } 
     } else { 
      $addWatchlist_bad_message = '<div class="alert alert-error">Error: could not connect to the database.</div.';?> 
      <script> 
       $('a.add-watchlist').trigger('click'); 
      </script><?php 
     } 
     require_once("db_close.php"); 
    } 

我的查詢,但是,似乎在SELECT語句中失敗,在添加新的監視列表中的監視列表索引並添加電影到新創建的監視列表之間。

回答

0

您將需要爲每個表編寫一個INSERT。

$mysqli->query("INSERT INTO watchlist_films (watchlist_id, film_id) 
    VALUES ('" . $watchlist_name['watchlist_id'] . "', '$rt_id')"); 
$mysqli->query("INSERT INTO watchlists ('watchlist_id') 
    VALUES (" . $watchlist_name['watchlist_id'] . ")"); 
+0

所以這會是「$查詢= INSERT INTO watchlist_films(watchlist_id,film_id)VALUES( '$ watchlist_name [' watchlist_id ']', '$ rt_id'),並插入到監視列表( 'watchlist_id' )VALUES($ watchlist_name ['watchlist_id']);「? – 2013-03-23 11:12:04

+0

幾乎是兩個單獨的查詢。你將不得不使用兩個查詢呼叫發送到服務器。 – Eelke 2013-03-23 11:15:26

2
try this 


$query1 = "INSERT INTO watchlist_films (watchlist_id, film_id) 
VALUES ('" . $watchlist_name['watchlist_id'] . "', '$rt_id')"; 

$query2= "INSERT INTO watchlists ('watchlist_id') 
VALUES (" . $watchlist_name['watchlist_id'] . ")"; 

$result = mysqli_multi_query($query1, $query2); 
+0

感謝您的幫助,我根據您的幫助更新了我的原文,提供新信息! – 2013-03-23 12:15:40