繁体   English   中英

从列表中选择数据库,然后在PHP中查询它

[英]Select database from a list and then query it in PHP

我想知道我的代码中缺少什么。

我有这个HTML代码:

<html>
  <head>
    <title>Human gene catalog</title>
  </head>
<body>
  <h1>Reference sequence and Gene Ontology catalog</h1>
  <p>
    <b>1.Search for a gene:</b>
    <form action=prueba.php method=post>
      <select name=organism> 
        <option value=1>Human</option> 
        <option value=2>Mouse</option> 
        <option value=3>Zebrafish</option> 
        <option value=4>Fruit fly</option> 
      </select>
      <label>Please select a gene:</label>
      <br/>
      <input type=text name=gene>
      <br/><br/>
      <input type=submit name=submit value=Submit>
    </form>
  </p>
</html>

这个PHP代码:

 <?php
   $gene = $_POST["gene"];
   $specie = $_POST["organism"];

   $enlace = mysqli_connect("localhost","root","******","refGene");
   if (mysqli_connect_errno()) {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

   if ($specie == 1) {
       mysqli_select_db($enlace,"refGene_human");
   } elseif ($specie == 2) {
       mysqli_select_db($enlace,"refGene_mouse");
   } elseif ($specie == 3) {
       mysqli_select_db($enlace,"refGene_zebrafish");
   } elseif ($specie == 4) {
       mysqly_select_db($enlace,"refGene_fruitfly");
   } else {
       echo "The gene is not in database";
   }

   $result = mysqli_query($enlace,"select * from (here I dont know what to put,if with one specie, here I put the name of the table) where name2 like '%$gene%'");

   echo "<h1>Gene Reference Results</h1>";

   echo "<table cellspacing=3 cellpadding=4 border=1 bgcolor=#dddddd>";
   echo "<tr align='center'><th>Transcript</th><th>Gene</th <th>Chromosome</th><th>Strand</th><th>Gene_Start</th><th>Gene_End</th><th>CDS_Start</th><th>CDS_End</th><th>ExonCount</th>";

   while ($extraido = mysqli_fetch_array($result)){
       echo "<tr>";
       echo "<td>".$extraido['name']."<br/>";
       echo "<td>".$extraido['name2']."<br/>";
       echo "<td align='center'>".$extraido['chrom']."<br/>";
       echo "<td align='center'>".$extraido['strand']."<br/>";
       echo "<td align='right'>".$extraido['txStart']."<br/>";
       echo "<td align='right'>".$extraido['txEnd']."<br/>";
       echo "<td align='right'>".$extraido['cdsStart']."<br/>";
       echo "<td align='right'>".$extraido['cdsEnd']."<br/>";
       echo "<td align='right'>".$extraido['exonCount']."<br/>";
   }
   echo "</table>";

   mysqli_free_result($result);
   mysqli_close($enlace);

基本上,我想要实现的是,在HTML文档中选择一个物种和一个基因名称,它将带您进入PHP,在此,根据物种的值(1,2,3,4)选择相应的数据库,然后查询它以查找一些信息。

我有正确的代码可以使它工作,但是只需要一个种类,而HTML中没有选择列表,PHP中没有if语句,但是我想使它在多个中工作。

我认为我应该在PHP的if语句之前声明一些变量,然后查询该变量,这应该告诉查询函数选择哪个表。

但是我不知道什么是正确的语法。

if ($specie == 1) {
    mysqli_select_db($enlace,"refGene_human");
    $database = 'refGene_human';
} 
elseif ($specie == 2) {
    mysqli_select_db($enlace,"refGene_mouse");
    $database = 'refGene_mouse';
} 
elseif ($specie == 3) {
    mysqli_select_db($enlace,"refGene_zebrafish");
    $database = 'refGene_zebrafish';
} 
elseif ($specie == 4) {
    mysqly_select_db($enlace,"refGene_fruitfly");
    $database = 'refGene_fruitfly'; 
} 
else { 
    echo "The gene is not in database";
}

$result = mysqli_query($enlace,"select * from '$database' where name2 like '%$gene%'");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM