-2

I'm trying to pull information from a 'SQL' database via a 'SUBMIT' form. The connection is made I presume as it's the same code as for the entry and that works just fine. I can display the result as a block of items but not integrate it in a table.

For now as the code is, it just returns a blank page...

I find many examples from a couple years ago but things have changed since..

Here is the code on the PHP page that should display the results in a table.

I've tried different ways of integrating the results to a table before coming here for help but all return the same result. I am fairly new to this.

 <?php

$servername = "localhost";
$username = "root";
$password = "Rmvs03ff";
$dbname = "EmployeeListing";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['submit-search'])) {
    $search = mysqli_real_escape_string($conn, $_POST['search']);
    $sql = "SELECT * FROM BasicEmployee WHERE EmployeeID LIKE '%$search%' OR FirstName LIKE '%$search%' OR LastName LIKE '%$search%' OR DoB LIKE '%$search%'";
    $result = mysqli_query($conn, $sql);
    $queryResult = mysqli_num_rows($result);

    echo "<br/>There are " .$queryResult. " matches found";

    echo "<table>
            <tr>
            <th>Employee I.D.</th>
            <th>Sex</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Date of Birth</th>
            </tr>";

    if ($queryResult > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo    "<tr>"
            echo    "<td><strong>
                    ".$row['EmployeeID']."
                    </strong></td>"
            echo    "<td>
                    ".$row['Prefix']."
                    </td>"
            echo    "<td><p>
                    ".$row['FirstName']."
                    </p></td>"
            echo    "<td><p>
                    ".$row['LastName']."
                    </p></td>"
            echo    "<td><p>
                    ".$row['DoB']."
                    </p></td>";
            echo "</table>";
        }

    } else {
        echo "<br/>No results found";
    }

}

echo "<br/>Approved";

$conn->close();

?>
   </body>
   </html>

I do have the opening html, head an bodies of course, didn't want to block up space with this...

SMH
  • 1,276
  • 3
  • 20
  • 40
danny26b
  • 463
  • 2
  • 13
  • This isn't an answer to the above "Reference - What does this error mean in PHP?" as I'm getting a blank page and not an error here .... and the log doesn't seem to be logging anything at the moment. – danny26b Nov 20 '17 at 01:22
  • Why don't I see semicolons at the end of each echo statement? And do you see that your table html is broken when there are no rows? – mickmackusa Nov 20 '17 at 06:17

1 Answers1

0

You need to echo the whole table once:

<?php

$servername = "localhost";
$username = "root";
$password = "Rmvs03ff";
$dbname = "EmployeeListing";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['submit-search'])) {
    $search = mysqli_real_escape_string($conn, $_POST['search']);
    $sql = "SELECT * FROM BasicEmployee WHERE EmployeeID LIKE '%$search%' OR FirstName LIKE '%$search%' OR LastName LIKE '%$search%' OR DoB LIKE '%$search%'";
    $result = mysqli_query($conn, $sql);
    $queryResult = mysqli_num_rows($result);

    echo "<br/>There are " .$queryResult. " matches found";

    $table = "<table>
            <tr>
            <th>Employee I.D.</th>
            <th>Sex</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Date of Birth</th>
            </tr>";

    if ($queryResult > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $table .= "<tr>";
            $table .= "<td><strong>
                    ".$row['EmployeeID']."
                    </strong></td>";
            $table .= "<td>
                    ".$row['Prefix']."
                    </td>";
            $table .= "<td><p>
                    ".$row['FirstName']."
                    </p></td>";
            $table .= "<td><p>
                    ".$row['LastName']."
                    </p></td>";
            $table .= "<td><p>
                    ".$row['DoB']."
                    </p></td>";
            $table .= "</table>";
        }

    } else {
        echo "<br/>No results found";
    }

}
echo $table;


echo "<br/>Approved";

$conn->close();

?>
   </body>
   </html>
SMH
  • 1,276
  • 3
  • 20
  • 40