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

时间:2015-03-16 04:46:24

标签: php

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

<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"/>

动作.php文件应该是什么?

3 个答案:

答案 0 :(得分:0)

<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();
     }

?>

答案 1 :(得分:0)

尝试使用类似的东西

文件路径

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

的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的

<?
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."'";


?>

答案 2 :(得分:0)

我修改了这样的@Rex Adrivan代码。 $theme1_data$theme2_data将是数组数组

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']);
}