我期待从单一表单中将数据发布到两个数据库表中。 我的数据库被安排为如下:将表单数据插入到两个数据库表中
数据库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
(从以前的选择查询)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语句中失败,在添加新的监视列表中的监视列表索引并添加电影到新创建的监视列表之间。
所以这会是“$查询= 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
几乎是两个单独的查询。你将不得不使用两个查询呼叫发送到服务器。 – Eelke 2013-03-23 11:15:26