-2

How can i exclude subject to be displayed from subject table that is already added in scheduler table? I have this code but it still shows up the subjects that are already added to scheduler table.

<?php
    $sem = $_GET['semChooser'];

    $res = mysql_query("SELECT * FROM scheduler WHERE semester = '$sem'") or die(mysql_errno());
   $row=mysql_fetch_array($res);

           $subName = $row['subject_name'];

        $res1 = mysql_query("SELECT * FROM subject WHERE semester IN ('$sem', 3) AND subject_name != '$subName'") or die(mysql_errno());
      while ($row=mysql_fetch_array($res1)) {

      echo "
         <tr class='active'> 
            <td align='center'>
              <button type='submit' data-dismiss='modal' class='btn btn-primary btn-block btn-xs subject'><em class='fa fa-plus'></em> Add</button>
            </td>
            <td>".$row['subject_code']."</td> 
            <td>".$row['subject_name']."</td> 
            <td>".$row['subject_type']."</td>
            <td>".$row['room_type']."</td>
            <td>".$row['subject_category']."</td>
            <td>".$row['year_level']."</td>  
            <td>".$row['units']."</td> 
         </tr> "; }
    ?>
  • 1
    Please be more specific about you question. If you are trying to just select specific columns from a table then use the column name rather than '*' operator. Specify what your tables are and what data you are trying to query. – bos570 Oct 06 '16 at 15:31
  • `if((condition) == x)){...}`. – Funk Forty Niner Oct 06 '16 at 16:02
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Oct 06 '16 at 16:03
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Oct 06 '16 at 16:03

1 Answers1

2

You could use a subselect and getting the result with one query only

      SELECT * 
      FROM subject 
      WHERE semester IN ('$sem', 3)  
      AND subject_name not in ( SELECT * FROM scheduler 
                                WHERE semester = '$sem' )
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107