-1

我希望从一个表单将数据发布到两个数据库表中。我的数据库排列如下:

数据库 1 - “观察名单”

  • watchlist_id
  • 用户身份
  • 姓名
  • 描述
  • 类别

数据库 2 - 'watchlist_films'

  • watchlist_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(从上一个查询中选择)ANDfilm_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 语句中失败,在将新的关注列表添加到关注列表索引和将电影添加到新创建的关注列表之间。

4

2 回答 2

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);
于 2013-03-23T11:32:05.033 回答
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'] . ")");
于 2013-03-23T11:09:50.643 回答