麻烦多个mysql查询

时间:2011-10-08 23:28:39

标签: php mysql

我在一个php文件中使用多个查询时遇到了一些麻烦。我想要创建的代码理想情况下会创建多个html下拉菜单,其中填充了来自不同表的数据。我的代码如下:

 echo "<form enctype='multipart/form-data' method='post'      action='http://www.vgiver.com/uploadpicture1.php' name='Giftgiver'>";
  $Wrap= @mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap"); 
 print "<p> Select a Wrapping Paper: \n";
 print "<Select name=\"Wrap_ID\">\n";

 while ($row2=mysql_fetch_assoc($Wrap)){
  $Wrap_ID=$row2['Wrap_ID'];
  $Wrap_Picture =$row2['Picture_Link'];
    print "<option value=$Wrap_ID>$Wrap_Picture \n";
  }

 $result= @mysql_query("select Friend_ID, tbl_Friends.Name from tbl_Friends inner join     tbl_Users on tbl_Friends.Access_ID=tbl_Users.Access_ID where tbl_Friends.Access_ID =     $myuserid"); 
  $Wrap= @mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap"); 
print "<p> Select a Friend: \n";
 print "<Select name=\"Friend_ID\">\n"; 

 while ($row=mysql_fetch_assoc($result)){
  $Friend_ID=$row['Friend_ID'];
  $Name =$row['Name'];
  print "<option value=$Friend_ID>$Name \n";
  }

但是,就像现在一样,此代码将仅为一组数据创建一个选择框。我先做哪个查询无关紧要,它会显示朋友列表的下拉菜单,或者我的包装纸的下拉菜单,但它不会同时显示。 任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

您忘记了结尾<select><p><option>标记。 它应该看起来像:

 <?php

  echo "<form enctype='multipart/form-data' method='post'      action='http://www.vgiver.com/uploadpicture1.php' name='Giftgiver'>";
  $Wrap = @mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
  print "<p> Select a Wrapping Paper:</p> \n";
  print "<Select name=\"Wrap_ID\">\n";

  while($row2 = mysql_fetch_assoc($Wrap)) {
        $Wrap_ID = $row2['Wrap_ID'];
        $Wrap_Picture = $row2['Picture_Link'];
        print "<option value=$Wrap_ID>$Wrap_Picture </option>\n";
  }
  echo '</select>';

  $result = @mysql_query("select Friend_ID, tbl_Friends.Name from tbl_Friends inner join     tbl_Users on tbl_Friends.Access_ID=tbl_Users.Access_ID where tbl_Friends.Access_ID =     $myuserid");
  $Wrap = @mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
  print "<p> Select a Friend: </p>\n";
  print "<Select name=\"Friend_ID\">\n";

  while($row = mysql_fetch_assoc($result)) {
        $Friend_ID = $row['Friend_ID'];
        $Name = $row['Name'];
        print "<option value=$Friend_ID>$Name </option>\n";
  }
  echo '</select>';

答案 1 :(得分:0)

在每个while循环之后,您需要结束<select>标记:

print "<select name=\"Wrap_ID\">\n";

while ($row2=mysql_fetch_assoc($Wrap)){
  $Wrap_ID=$row2['Wrap_ID'];
  $Wrap_Picture =$row2['Picture_Link'];
  print "<option value=$Wrap_ID>$Wrap_Picture</option> \n";
}

print '</select>';

您还应该像上面的示例中一样正确地结束您的<option>代码

答案 2 :(得分:0)

您忘了关闭selectoption

 print "<p> Select a Wrapping Paper: \n";
 print "<Select name=\"Wrap_ID\">\n";

 while ($row2=mysql_fetch_assoc($Wrap)){
  $Wrap_ID=$row2['Wrap_ID'];
  $Wrap_Picture =$row2['Picture_Link'];
    print "<option value=$Wrap_ID>$Wrap_Picture</option>\n";
  }
 print "</select>"; //<-------add this

编辑:

也是你的段落。 HTML要求关闭标记:

<tag>content</tag>

所以你会:

 print "<p> Select a Wrapping Paper: </p> \n";

而不是:

 print "<p> Select a Wrapping Paper: \n";

您的完整代码应为:

 echo "<form enctype='multipart/form-data' method='post'      action='http://www.vgiver.com/uploadpicture1.php' name='Giftgiver'>";
  $Wrap= @mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap"); 
 print "<p> Select a Wrapping Paper:</p> \n";
 print "<Select name=\"Wrap_ID\">\n";

 while ($row2=mysql_fetch_assoc($Wrap)){
  $Wrap_ID=$row2['Wrap_ID'];
  $Wrap_Picture =$row2['Picture_Link'];
    print "<option value=$Wrap_ID>$Wrap_Picture</option> \n";
  }
 print "</select>";

 $result= @mysql_query("select Friend_ID, tbl_Friends.Name from tbl_Friends inner join     tbl_Users on tbl_Friends.Access_ID=tbl_Users.Access_ID where tbl_Friends.Access_ID =     $myuserid"); 
  $Wrap= @mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap"); 
print "<p> Select a Friend: </p> \n";
 print "<Select name=\"Friend_ID\">\n"; 

 while ($row=mysql_fetch_assoc($result)){
  $Friend_ID=$row['Friend_ID'];
  $Name =$row['Name'];
  print "<option value=$Friend_ID>$Name</option> \n";
  }
print "</select>";

额外提示:

当您不需要对变量进行解析时,请使用单引号而不是双引号。

使用echo代替print

答案 3 :(得分:0)

您没有关闭<selects>。第二组数据在那里,但HTML忽略它。尝试查看您将看到的文件来源。