Php 使用复选框从多个表中检索数据

Php 使用复选框从多个表中检索数据,php,Php,我在mysql中有一个数据库,我使用php将数据从数据库显示到web。但由于我是php新手,我有一个问题。我的数据库中有多个表,比如说两个表,每个表代表不同的主题。因此,接下来我必须使用这两个主题添加复选框。当我选中复选框时,应提取所选主题的值 <input type="checkbox" name="search" value="theme1_info"/> cancer &nbsp &nbsp <input type="checkbox" name="sea

我在mysql中有一个数据库,我使用php将数据从数据库显示到web。但由于我是php新手,我有一个问题。我的数据库中有多个表,比如说两个表,每个表代表不同的主题。因此,接下来我必须使用这两个主题添加复选框。当我选中复选框时,应提取所选主题的值

<input type="checkbox" name="search" value="theme1_info"/> cancer &nbsp &nbsp <input type="checkbox" name="search" value="theme2_info"/> diabetes
<input type= "submit" value= "Search" name="sub"/>
action.php文件应该是什么

<form method="GET">
        <fieldset>
                <legend>Select a theme</legend>
                        <input type="checkbox" name="theme1" value="Name1" />Theme1 <br />
                        <input type="checkbox" name="theme2" value="1" />Theme2<br />
                        <input type="checkbox" name="theme3" value="1" />Theme3<br />
                        <input type="submit" value="Submit now" />
        </fieldset>
</form>
action.php

<?php
    $theme1 = $_GET['theme1'];
    if($theme1 == "Name1"){
      // connect to the database
      $ThemeName = $theme1;
      $servername = "localhost";
      $username = "username";
      $password = "password";
      $dbname = "myDB";

      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      } 

      $sql = "SELECT  * FROM ".$ThemeName."";
      $result = $conn->query($sql);

      if ($result->num_rows > 0) {
          // output data of each row
          while($row = $result->fetch_assoc()) {
              var_dump($row);
          }
      } else {
          echo "0 results";
      }
      $conn->close();
     }

?>
<?
if(issset($_REQUEST['theme1_info']))
{
    $theme=$_REQUEST['theme1_info'];
}else
{
$theme=$_REQUEST['theme2_info'];

}
 //you need to insert db connection

 //if your table is same as the theme name 
 $query ="SELECT * FROM '".$theme."'";


?>

试着用这样的东西

文件路径

文件夹/index.html 文件夹/action.php

<?php
    $theme1 = $_GET['theme1'];
    if($theme1 == "Name1"){
      // connect to the database
      $ThemeName = $theme1;
      $servername = "localhost";
      $username = "username";
      $password = "password";
      $dbname = "myDB";

      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      } 

      $sql = "SELECT  * FROM ".$ThemeName."";
      $result = $conn->query($sql);

      if ($result->num_rows > 0) {
          // output data of each row
          while($row = $result->fetch_assoc()) {
              var_dump($row);
          }
      } else {
          echo "0 results";
      }
      $conn->close();
     }

?>
<?
if(issset($_REQUEST['theme1_info']))
{
    $theme=$_REQUEST['theme1_info'];
}else
{
$theme=$_REQUEST['theme2_info'];

}
 //you need to insert db connection

 //if your table is same as the theme name 
 $query ="SELECT * FROM '".$theme."'";


?>
index.html

<form method="post" aaction="action.php">
<input type="checkbox" name="search" value="theme1_info"/> cancer &nbsp &nbsp <input type="checkbox" name="search" value="theme2_info"/> diabetes
<input type= "submit" value= "Search" name="sub"/>

</form>
action.php

<?php
    $theme1 = $_GET['theme1'];
    if($theme1 == "Name1"){
      // connect to the database
      $ThemeName = $theme1;
      $servername = "localhost";
      $username = "username";
      $password = "password";
      $dbname = "myDB";

      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      } 

      $sql = "SELECT  * FROM ".$ThemeName."";
      $result = $conn->query($sql);

      if ($result->num_rows > 0) {
          // output data of each row
          while($row = $result->fetch_assoc()) {
              var_dump($row);
          }
      } else {
          echo "0 results";
      }
      $conn->close();
     }

?>
<?
if(issset($_REQUEST['theme1_info']))
{
    $theme=$_REQUEST['theme1_info'];
}else
{
$theme=$_REQUEST['theme2_info'];

}
 //you need to insert db connection

 //if your table is same as the theme name 
 $query ="SELECT * FROM '".$theme."'";


?>

我已经像这样修改了@Rex Adrivan代码$ME1_数据和$ME2_数据将是数组的数组

function getSelectedTheme($theme) {
    $sql = "SELECT  * FROM " . $theme . "";
    $result = $conn->query($sql);
    $themeData = array();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $themeData[] = $row;
        }
    }
    return $themeData;
}

if(isset($_GET['theme1_info'])){
    $theme1_data = getSelectedTheme($_GET['theme1_info']);
}
if(isset($_GET['theme2_info'])){
    $theme2_data = getSelectedTheme($_GET['theme2_info']);
}

hello Rex感谢您的回复,但这里我不想打印任何消息,我想从多个表中提取数据。@Jyoti Sharma如果选中复选框,您可以发出数据库连接并使用mysql选择表。当用户同时选中这两个复选框时会发生什么?使用单选或选择元素。如果用户同时选中这两个复选框,则两个表中的数据都会出现。伙计们,我想我无法让你们理解我的问题,实际上我的数据库中有9个表,所以会有9个复选框。我想要的是根据选中的复选框从这些表中提取公共信息。例如,如果我单击2个复选框,那么它应该返回这两个选定表之间的公共值。我希望这次你能帮上忙。谢谢如果喜欢,那么用array like命名复选框,那么您将拥有所有选定的主题,并使用主题名创建表。连接选中复选框的元素并进行查询