0

I made a function and passed values in it but it does not give any output. Instead of writing the same code for fetching data from different tables, I want to make a function so that just by passing string values of table name code could be executed. Here is my code.

        function productTableQuery($selectTable) {

          $query = "SELECT * FROM $selectTable";
          $result = mysqli_query($connection, $query);

          while ($row = mysqli_fetch_assoc($result)) {

            $serialNumber = $row['serial_number']; 
            $particulars = $row['particulars']; 

            echo "<option value='$serialNumber'>$particulars</option>";

          }

        }

        switch($product_table) {
            case 'insert':
                productTableQuery("product_insert");

            break;

            default:
              header("Location: index.php");
              break;
          }
Dipanshu Chaubey
  • 880
  • 1
  • 9
  • 18

1 Answers1

-1

Try this. I have edited condition in while loop. And also you forgot to include 'select'
tag.

function productTableQuery($selectTable) {

          $query = "SELECT * FROM $selectTable";
          $result = mysqli_query($connection, $query);
          echo "<select name='dropDown'>";
          while ($row = mysqli_fetch_array($result)) {

            $serialNumber = $row['serial_number']; 
            $particulars = $row['particulars']; 

            echo "<option value='$serialNumber'>$particulars</option>";

          }
        echo "</select>";
        }

        switch($product_table) {
            case 'insert':
                productTableQuery("product_insert");

            break;

            default:
              header("Location: index.php");
              break;
          }
CaffeinatedCod3r
  • 821
  • 7
  • 14