我希望从单个表单将数据发布到两个数据库表中。 我的数据库安排如下:
数据库1 - '关注列表':
数据库2 - 'watchlist_films':
我当前的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
(从上一个查询中选择)和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语句中失败,在将新的Watchlist添加到Watchlist索引并将电影添加到新创建的监视列表之间。
答案 0 :(得分: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);
答案 1 :(得分: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'] . ")");