1

我需要在我的数据输入页面中添加另一个下拉菜单,并想出一种在两个表中显示日期的方法,但它在每个表中都加倍,即显示艺术家下拉菜单:

将 N 添加到 X
将 N 添加到 X
空气
Air
Amon Tobin
Amon Tobin
等。

贡献者下拉显示:

乔吹
乔吹
迪克一团
迪克一团
等。

这是我将表格数据翻倍的地方:

SELECT artists.id, artists.artists_name, contributors.id, contributors.contributors_name FROM artists, contributors ORDER BY artists.artists_name, contributors.contributors_name";

这里是上下文:

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO news (artistid, contributorid, title, blog_entry, rating) VALUES (%s, %s, %s, %s, %s)",
                   GetSQLValueString($_POST['artistid'], "text"),
                   GetSQLValueString($_POST['contributorid'], "text"),
                   GetSQLValueString($_POST['title'], "text"),
                   GetSQLValueString($_POST['blog_entry'], "text"),
                   GetSQLValueString($_POST['rating'], "text"));

mysql_select_db($database_em, $em);
$Result1 = mysql_query($insertSQL, $em) or die(mysql_error());

$insertGoTo = "add_post.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_em, $em);
$query_listPosts = "SELECT artists.id, artists.artists_name, contributors.id, contributors.contributors_name FROM artists, contributors ORDER BY artists.artists_name, contributors.contributors_name"; 
$listPosts = mysql_query($query_listPosts, $em) or die(mysql_error());
$row_listPosts = mysql_fetch_assoc($listPosts);
$totalRows_listPosts = mysql_num_rows($listPosts);
?>

这是我在页面中呈现它的方式:

<p>
<label for="artistid">Artist:</label>
</p>
<p><select name="artistid">
<option value="0">Not registered</option>
<?php
do {  
?>
<option value="<?php echo $row_listPosts['id']?>"><?php echo $row_listPosts['artists_name']?></option>
<?php
} while ($row_listPosts = mysql_fetch_assoc($listPosts));
$rows = mysql_num_rows($listPosts);
if($rows > 0) {
  mysql_data_seek($listPosts, 0);
  $row_listPosts = mysql_fetch_assoc($listPosts);
}
?>
</select></p>
<p>
<label for="contributorid">Contributor:</label>
</p>
<p><select name="contributorid">
<option value="0">Not registered</option>
<?php
do {  
?>
<option value="<?php echo $row_listPosts['id']?>"><?php echo $row_listPosts['contributors_name']?></option>
<?php
} while ($row_listPosts = mysql_fetch_assoc($listPosts));
$rows = mysql_num_rows($listPosts);
if($rows > 0) {
  mysql_data_seek($listPosts, 0);
  $row_listPosts = mysql_fetch_assoc($listPosts);
}
?>
</select></p>

虽然我现在可以处理这种加倍,但当我向我的数据输入页面添加更多下拉列表时,它会成为一个问题,所以任何想法如何解决这个问题,以便每个下拉只有一个数据实例而不是加倍会不胜感激。

4

1 回答 1

1

这是由您的查询引起的。您将 2 个没有关系的表连接在一起,因此它正在创建多行,匹配artistscontributors,并且id正在合并。

我在http://sqlfiddle.com/#!2/d7962/2/0创建了这个问题

这是我的建议-

把1个查询改成2个查询,为了简化代码,把do-while循环改成while()循环

查询-

$query_Artists = "SELECT id, artists_name FROM artists ORDER BY artists_name";
$query_Contributors = "SELECT id,contributors_name FROM contributors ORDER BY contributors_name"; 
$listArtists = mysql_query($query_Artists, $em) or die(mysql_error());
$listContributors = mysql_query($query_Contributors, $em) or die(mysql_error());

下拉菜单-

<p>
<label for="artistid">Artist:</label>
</p>
<p><select name="artistid">
<option value="0">Not registered</option>
<?php
if(mysql_num_rows($listArtists) > 0) {
while ($artist_dropdown = mysql_fetch_assoc($listArtists)) {  
?>
<option value="<?php echo $artist_dropdown['id']?>"><?php echo $artist_dropdown['artists_name']?></option>
<?php
}
}
?>
</select></p>
<p>
<label for="contributorid">Contributor:</label>
</p>
<p><select name="contributorid">
<option value="0">Not registered</option>
<?php
if(mysql_num_rows($listContributors) > 0) {
while ($contributors_dropdown = mysql_fetch_assoc($listContributors)) {  
?>
<option value="<?php echo $contributors_dropdown['id']?>"><?php echo $contributors_dropdown['contributors_name']?></option>
<?php
}
}
?>
</select></p>
于 2012-11-26T20:30:55.287 回答