简体   繁体   中英

Select database from a list and then query it in PHP

I would like to know what I'm missing in my code.

I have this HTML code:

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

And this PHP code:

 <?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);

Basically what I want to achieve is that, selecting a specie and a gene name in the HTML document, it gets you to the PHP, where, depending on the value of specie (1,2,3,4) it selects the corresponding database, and then querying it to find some information.

I have the right code to make it work but just with one specie, without the select list in the HTML and the if statements in the PHP, but I would like to make it work in more than one.

I think I should declare some variable before the if statement in the PHP and then querying over this variable which should tell the query function what table to choose.

But I don't know what could be the right syntax.

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%'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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