搜索表中任意两列的组合

时间:2015-05-12 15:45:11

标签: mysql sql

我想根据4列的任意两个组合在表格中搜索 1-INSID 2- CRN 3-TERM 4-CID。我认为它是六个组合* 2因为val1可能意味着vla2。有更好的解决方案吗?

<?php 
    // define variables and set to empty values 
    $val1= $val2="" ;

    if (isset($_POST['submit'])){
        $val1=test_input($_POST["Val1"]);
        $val2=test_input($_POST["Val2"]); 
    } 

    function test_input($data) {
        $data=trim($data);
        $data=stripslashes($data);
        $data=htmlspecialchars($data);
        return $data;
    } 
?>


<div class="container">
    <div class="row">
        <form name="myForm" class="col s12" method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>">
            <div class="row">
                <div class="input-field col s2">
                    <p>Enter any combination of:<br>1)CRN<br>2)INSID<br>3)CID<br>4)TERM</p>
                    <input id="Val1" type="text" class="validate" name="Val1">
                    <label for="Val1">First key to search for</label>
                </div>
                <div class="input-field col s2">
                    <input id="Val2" type="text" class="validate" name="Val2">
                    <label for="Val2">Second key to search for</label>
                </div>
            </div>
            <button class="waves-effect waves-light btn" type="submit" name="submit">Search
                <i class="mdi-content-send right"></i>
            </button>
        </form>
    </div>
</div>
<?php       
    if (isset($_POST[ 'submit'])) { 
        if ($val1 !="" and $val2 !="" ) { 
            $con=mysql_connect( 'localhost', 'root', ''); 
            if (!$con) { 
                die( 'Could not connect: ' . mysqli_connect_error()); 
            } 
            $DBName="ICS324" ; 
            //echo 'Connected successfully'; 
            if (!mysql_select_db($DBName, $con)) { 
                die( 'Could not select database SWE363'); 
            } 
            //echo ( '</br> DB students selected successfully');
            mysql_query("SELECT * FROM  `SECTION` 
                                WHERE  
                                (
                                    `CRN` ='$val1'
                                     AND  `TERM` =  '$val2'
                                )

                                ||

                                (
                                     `CRN` ='$val1'
                                     AND  `INSID` =  '$val2'  
                                )

                                ||

                                (
                                     `CRN` ='$val1'
                                     AND  `CID` =  '$val2'  
                                )

                                ||

                                (
                                     `INSID` ='$val1'
                                     AND  `CID` =  '$val2'  
                                )

                                ||

                                (
                                     `TERM` ='$val1'
                                     AND  `CID` =  '$val2'  
                                )

                                ||

                                (
                                     `TERM` ='$val1'
                                     AND  `INSID` =  '$val2'
                                )
                                    (
                                    `CRN` = '$val2'
                                     AND  `TERM` =  '$val1'
                                )

                                ||

                                (
                                     `CRN` ='$val2'
                                     AND  `INSID` =  '$val1'  
                                )

                                ||

                                (
                                     `CRN` ='$val2'
                                     AND  `CID` =  '$val1'  
                                )

                                ||

                                (
                                     `INSID` ='$val2'
                                     AND  `CID` =  '$val1'  
                                )

                                ||

                                (
                                     `TERM` ='$val2'
                                     AND  `CID` =  '$val1'  
                                )

                                ||

                                (
                                     `TERM` ='$val2'
                                     AND  `INSID` =  '$val1'
                                )");
            echo "The table has the following records:";
            // query here 
            $result=mysql_query( "SELECT * FROM PERSON WHERE 1") or die ( "Error in query: $query. ".mysql_error());



            echo "<table border='1'>
            <tr>
            <th>CRN</th>
            <th>TERM</th>
            <th>INSID</th>
            <th>CID</th>
            <th>ACTIV</th>
            <th>DAYS</th>
            <th>BLD</th>
            <th>ROOM</th>
            <th>STime</th>
            <th>ETime</th>
            <th>Enrolled</th>
            <th>MaxEnrolled</th>
            </tr>"; 
            while($row=mysql_fetch_array($result,MYSQL_ASSOC)) {
                print "<tr>
                <td>".$row['CRN']. "</td>
                <td>".$row['TERM']. "</td>
                <td>".$row['INSID']. "</td>
                <td>".$row['CID']. "</td>
                <td>".$row['ACTIVITY']. "</td>
                <td>".$row['DAYS']. "</td>
                <td>".$row['BUILDING']. "</td>
                <td>".$row['ROOM']. "</td>
                <td>".$row['START_TIME']. "</td>
                <td>".$row['END_TIME']. "</td>
                <td>".$row['NUMBER_OF_ENROLLED_STUDENTS']. "</td>
                <td>".$row['MAXIMUM_LIMIT_ON_ENROLLMENT']. "</td>
                </tr>"; 
            } 
            echo "</table>";
            mysql_close($con); 
            } else {
                echo "Please fill out all the fields !"; 
            } 
    } 
?>

0 个答案:

没有答案