I have a html page with an input box. I am trying to retrieve data from the database based on that input and send it back to the original html page in the form of a table using php. I created the php in an external file and it was displaying the correct values in the browser but my issue is trying to display the php on the html page.
I enabled my server to parse html files and have put the php within the html file but still was not working. When I entered a value and submit the page reloads but nothing is displayed.
If anyone knows the best way of doing this I would appreciate the advice. I have read numerous threads and none seem to work.
PHP:
<?php
define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
$studentnum = $_POST['studentnum'];
$sql = "SELECT * FROM test WHERE number LIKE '%$studentnum%'";
$result=mysqli_query($conn, $sql);
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr>
<th>Name</th>
<th>Number</th>
<th>Floor</th>
<th>Room</th>
<th>Message</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['number'];
echo "</td><td>";
echo $row['floor'];
echo "</td><td>";
echo $row['room'];
echo "</td><td>";
echo $row['message'];
echo "</td></tr>";
}
echo "</table>";
mysqli_close($conn);
?>
HTML:
<form action="test.php" method="post">
<label>Enter Student Number:</label>
<input name="studentnum" type="number" placeholder="Type Here">
<br>
<br>
<input type="submit" value="Enter">
</form>