基于php mysql

时间:2015-10-02 11:32:35

标签: php mysql

嗯,首先,我的数据库结构很荒谬。不过,我需要一些帮助。不知何故,我实现了一个表,它从4个不同的表中获取输入。

<h2>SEARCH PROTEIN</h2>
            <div class="panel panel-primary">
            <div class="panel-heading">SELECT PROTEIN NAME FROM LIST</div>
            <div class="panel-body">
            <form name="dropdown" action="http://test.php" method="post">
            <select class="form-control" data-style="btn-primary" name="p_ID">
<?php
//provide your hostname, username and dbname
$host="localhost"; 
$username="root";  
$password="";
$db_name="myDB"; 
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name");

$sql = "select p_ts from protein_search1";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo "<option value=$row[p_ts]>$row[p_ts]</option>";
}

$sql = "select p_ts from protein_search2";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo "<option value=$row[p_ts]>$row[p_ts]</option>";
}

$sql = "select p_ts from protein_search3";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo "<option value=$row[p_ts]>$row[p_ts]</option>";
}

$sql = "select p_ts from protein_search4";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo "<option value=$row[p_ts]>$row[p_ts]</option>";
}
?>

    </select><br><br>
            <button class="btn btn-primary center-block btn-lg" type="submit" >Search</button>
                </form> 

            </div>
        </div>
    </div>

这给了我一个下拉菜单,其中包含p_ts id,来自所有4个表。

现在我以某种方式,想要进行查询,如果,从table1找到p_ts将其发送到某个页面,比如说1.php,如果在table2中找到,那么到2.php,依此类推。

这是我尝试的东西,效率很低。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
    {
      die("Connection failed: " . $conn->connect_error);
    }
else 
{
if(isset($_POST[p_ts]) )
{
if($result->num_rows == 0){
$sql = "select *, '1' as php from protein_search1 where p_ts like '$_POST[p_ts]%'
union
select *, '2' as php from protein_search2 where p_ts like '$_POST[p_ts]%'
union
select *, '3' as php from protein_search3 where p_ts like '$_POST[p_ts]%'
union
select *, '4' as php from protein_search 4where p_ts like '$_POST[p_ts]%' ";
$result = $conn->query($sql) or die($conn->error.__LINE__);
$row=$result->fetch_assoc();
header("Location: ".$row['php'].".php"); }
else 
        {
             echo "0 results";
        }}}

$conn->close();
?>

建议我继续使用这个“if-else声明”。

0 个答案:

没有答案