0

The first dropdown should select all the semester data, and it works. For the second dropdown, it is supposed to select all the student_prg data but it doesn't work (dropdown list empty).

Is there any method to make it work? Please let me if there is any other way to make both the dropdown work.

config.php

<?php 

define('DBINFO','mysql:host=localhost;dbname=marketing_system');
define('DBUSER','root');
define('DBPASS','');

$conn = mysqli_connect("localhost","root","","marketing_system");

?>

testreport.php

<div class="modal-body">
        <div class="form-group">
              <label for="title">Select Semester:</label>
              <select name="semester" class="form-control">
                  <option value="">--- Select Semester ---</option>

                  <?php
                    require('../setting/config.php');
                      $query = "SELECT DISTINCT semester FROM marketing_data ORDER BY semester DESC"; 
                      $do = mysqli_query($conn, $query);
                      while($row = mysqli_fetch_array($do)){
                          echo '<option value="'.$row['student_matric'].'">'.$row['semester'].'</option>';
                      }
                  ?>
              </select>
          </div>
          <div class="form-group">
              <label for="title">Select Programme:</label>
              <select name="prg" class="form-control">
                <option value="">--- Select Programme ---</option>

                <?php
                require('../setting/config.php');
                      $query2 = "SELECT DISTINCT student_prg FROM marketing_data ORDER BY student_prg DESC"; 
                      $do = mysqli_query($conn, $query2);
                      var_dump($do); die();
                      while($row = mysqli_fetch_array($do)){
                          echo '<option value="'.$row['student_matric'].'">'.$row['student_prg'].'</option>';
                      }
                ?>
              </select>
          </div>

Screenshot

Database

mysql> describe marketing_data;
+---------------+------------------+------+-----+---------+--------------------+
| Field         | Type                 | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+--------------------+
| student_matric| varchar(10) unsigned | NO   | PRI | NULL    | auto_increment |
| student_prg   | text unsigned        | YES  |     | NULL    |                |
| semester      | varchar(10)          | YES  |     | NULL    |                |
| intake_year   | int(10)              | YES  |     | NULL    |                |
| student_city  | text                 | YES  |     | NULL    |                |
| city_lat      | varchar(20)          | YES  |     | NULL    |                |
| city_long     | varchar(20)          | YES  |     | NULL    |                |
| student_state | text                 | YES  |     | NULL    |                |
| state_code    | varchar(100)         | YES  |     | NULL    |                |
+---------------+------------------+------+-----+---------+--------------------+
  • $query2 = "SELECT DISTINCT student_prg , student_matric FROM marketing_data....because this field was missing and you call it here value="'.$row['student_matric'].'" – Hardood May 30 '20 at 08:42
  • How many permutations are there – Strawberry May 30 '20 at 08:45
  • Are the dropdowns dependent? – Strawberry May 30 '20 at 08:47
  • Hi @Hardood , still the second dropdown list is empty. Even I put as SELECT * FROM marketing data and call the value as in the code for both dropdown, the second dropdown still empty. – Hasviny Rao May 30 '20 at 08:47
  • Hi @Strawberry , I've tried dependent as well but still same issue.. – Hasviny Rao May 30 '20 at 08:48
  • 1
    after this line $do = mysqli_query($conn, $query2); please var_dump($do); die(); and then see the result to make sure you have data in your resultset – Hardood May 30 '20 at 08:51
  • Done as per edited, still empty... – Hasviny Rao May 30 '20 at 08:56
  • 1
    Why did you write `require('../setting/config.php');` a second time? You've already required it previously, you don't need to add it again. Maybe it even causes an error. – ADyson May 30 '20 at 09:04
  • Does it change anything, in the second dropdown, if you comment the query of the first dropdown? – Roberto Caboni May 30 '20 at 09:05
  • Thank you, @ADyson .. It works after remove the require in the second dropdown... Never thought this would be an issue.. Thanks everyone. – Hasviny Rao May 30 '20 at 09:14
  • @ADyson nice shot. That's because the required code includes another call of `$conn = mysqli_connect("localhost","root","","marketing_system");`, so the previously valid $conn handler is overwritten by what is likely some "already connected" error code. – Roberto Caboni May 30 '20 at 09:22
  • Yes, noted well... Thank you everyone..... – Hasviny Rao May 30 '20 at 09:29

1 Answers1

0

You are calling

require('../setting/config.php')

twice, which is unnecessary and also will cause an issue. Remove that and your problem will be solved.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    Feel free to incorporate my comment, trying to explain _why_ the second require causes the bug. I would also mention that connect returns false on failure, and that the error can be checked with `mysqli_connect_error` – Roberto Caboni May 30 '20 at 09:37
  • @RobertoCaboni thanks but actually I'd say that rather than littering the code with `if` statements and calls to `mysqli_connect_error` and `mysqli_error` (which will be repetitive and make the code flow much more complicated to implement and understand) it's better to just [get mysqli to throw exceptions when SQL errors occur](https://stackoverflow.com/a/14578644/5947043) and then handle/log those errors like any other error in the application. – ADyson Jun 05 '20 at 08:03