我无法在PHP中执行我的代码

时间:2017-06-14 10:49:46

标签: php

    <form method="post" action="newrs.php" target="_blank" enctype="multipart/form-data" >
<table>
<tr>
<td><font style="font-size:20px">Snp Id:</font></td>
<td><input  type="text" name="isnp_id" id="idsnp_id" placeholder="Ex:SCcLG07_1001342" size=20 style="font-size:18px"></td>
</tr>
<tr>
<td><font style="font-size:20px">Gene Id:</font></td>
<td><input  type="text" name="igene" id="idgene" placeholder="Ex:C.cajan_17400" size=20 style="font-size:18px"></td>
</tr>
<tr>
<td><font style="font-size:20px">Chromosome:</font></td>
<td><input  type="text" name="ichr" id="idchr" placeholder="Ex:CcLG07" size=20 style="font-size:18px"></td> 
<td> position from </td>
<td><input type="text" name="posstart" id="idposstart" placeholder="Ex: 1" size="20"> to <input type="text" name="posend" id="idposend" placeholder="Ex:100" size="20">
</td>
</tr>
<tr>
<td><font style="font-size:20px">Effect:</font></td> <td><input  type="text" name="ieff" id="ideff" placeholder="Ex:Low, High, Moderate" size=20 style="font-size:18px"></td> 
 </tr>
</table> 
<br/>
<span><font style="font-size:22px;color:green">Options:</font></span><br/>
<table>
<tr>
<td><input type="checkbox" name="cb" value="cb_class" /></td><td><font style="font-size:20px">Classification:</font></td>
<td><select style="font-size:18px" name="sclass" >
<option value="all" selected="selected">Select</option>
<option value="intergenic">Intergenic</option>
<option value="intronic">Intronic</option>
<option value="non_synonymous_coding">Non Synonymous Coding</option>
<option value="synonymous_coding">Synonymous Coding</option>
</select> </td>
</tr>
<tr>
<td><input type="checkbox" name="cb" value="cb_vtype" /></td><td><font style="font-size:20px">Varation Type:</font></td>
<td><select style="font-size:18px" name="svtype" >
<option value="all" selected="selected">Select</option>
<option value="snp">Snp</option>
<option value="insertion">Insertion</option>
<option value="deletion">Deletion</option>
</select> </td>
</tr>
<tr>
<td><input type="checkbox" name="cb" value="cb_fc" /></td><td><font  style="font-size:20px">Functional Class:</font></td>
<td><select style="font-size:18px" name="sfunclass" >
<option value="all" selected="selected">Select</option>
<option value="missense">Missense</option>
<option value="nonsense">Nonsense</option>
<option value="silent">silent</option>
</select>
</td>
</tr>

<tr> 
<td></td><td></td>
<td><input type="reset" name ="reset" value="Reset" style="font-size:18px">
<input type="submit" name="submit" value="Search" style="font-size:18px">  </td>
</tr>
</table>
</form>
  php code starts here:
  <?php 
 include_once 'connect.php';
 if(isset($_POST['submit']))
{
 $selected_cb=$_POST['cb'];
 if(($gene=$_POST['igene']) && ($selected_cb=='cb_class') && ($selected_cb=='cb_vtype'))
 {
   $selectc=$_POST['sclass'];
    if($selectc=='intergenic')
    {
     $selectv=$_POST['svtype'];
        switch($selectv)
        {
         case "snp":        
                 $sql="select snp_eff.*,variations_categorised.variation_type from snp_eff inner join variations_categorised on snp_eff.snp_id = variations_categorised.snp_id where snp_eff.classification ='INTERGENIC' AND snp_eff.transcript like '%$gene%' AND variations_categorised.variation_type='SNP' LIMIT 1000";
             break;
             case "insertion":          
                 $sql="select snp_eff.*,variations_categorised.variation_type from snp_eff inner join variations_categorised on snp_eff.snp_id = variations_categorised.snp_id where snp_eff.classification ='INTRONIC' AND snp_eff.transcript like '%$gene%' AND variations_categorised.variation_type='INSERTION' LIMIT 1000";
         break;
         case "deletion":           
             $sql="select snp_eff.*,variations_categorised.variation_type from snp_eff inner join variations_categorised on snp_eff.snp_id = variations_categorised.snp_id where snp_eff.classification ='NON_SYNONYMOUS_CODING' AND snp_eff.transcript like '%$gene%' AND variations_categorised.variation_type='DELETION' LIMIT 1000";
                     break;
        }
    }
  }
  }
 $counter=0;
if($result = pg_query($link, $sql) ) {
if(pg_numrows($result) > 0) {
?>
  <table id="t01" >
  <tr >
 <th>sno</th>
 <th>chr</th>
 <th>pos</th>
 <th>snp_id</th>
 <th>ref_base</th>
 <th>alt_base</th>
<th>classification</th>
<th>effect</th>
 <th>functional class</th>
<th>variation type</th>
 <th>Gene ID</th>
 </tr>
   <?php
    while($row = pg_fetch_array($result)){
    ?>
        <tr>
            <td><?php echo ++$counter ;?> </td>     
            <td><?php echo $row['chr'] ;?> </td>
            <td><?php echo $row['position'] ;?> </td>
            <td><?php echo $row['snp_id'] ;?>  </td>                                   
            <td><?php echo $row['ref'] ;?> </td>
            <td><?php echo $row['alt'] ;?> </td>

            <td><?php echo $row['classification'] ;?> </td>
            <td><?php echo $row['effect'] ;?> </td>
            <td><?php echo $row['functional_class'] ;?> </td>
            <td><?php echo $row['variation_type'] ;?> </td> 
            <td><?php echo $row['transcript'] ;?> </td> 
          </tr>

         <?php
      }
   } 
  else{
      echo  " No records matching your query were found.";
     }
   }
 ?>   
 </table> 

我在搜索三种组合的记录时没有得到任何结果。在apache日志中,它显示错误:

  

未定义的变量:sql

我无法弄清楚我在这段代码中遗漏了什么。

1 个答案:

答案 0 :(得分:0)

您收到该错误的原因是因为$sql变量始终未设置。

当您访问以下代码并且页面尚未提交时,$sql变量无处可寻:

if($result = pg_query($link, $sql)) {

您应该检查表单是否已提交,然后检查数据库结果。

if(isset($_POST['submit'] && $result = pg_query($link, $sql)) {