Wanted to know the correct syntax for my code. It is a php code that extracts data from a mySQL database. This code should I displayed the latest data inputted for servername, contact, location, vlan, servicerequest. My php code is not working:
<!DOCTYPE html>
<html>
<div id="button"><a href="http://10.90.4.71/cfg2html_outputs/Checklist/php/index.php">Back to Checklist Page</a></div>
<head>
<title>TESTER</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
<style>
table, th, td
{
border: 1px solid grey
}
li.a {list-style-position:inside;}
</style>
</head>
<body>
<table>
<h2><b>TESTER</b></h2>
<table width="1900" align="left">
<tr>
<th width="1900" align="left" colspan="3" bgcolor="#D3D3D3"><b>SERVER INFORMATION</b></th>
</tr>
<tr>
<td width="200" align="left">Server name:</td>
<td width="1700" align="left" colspan="2">
<?php
$conn = mysqli_connect("localhost", "username", "password", "test");
if ($conn-> connect_error) {
die("Connection failed:". $conn-> connect_error);
}
$sql = "SELECT servername, contact, location, vlan, servicerequest from info";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
echo $row["servername"] ."</td></tr><tr><td width="200" align="left">Contact:</td><td width="1700" align="left" colspan="2">". $row["contact"] ."</td></tr><tr><td width="200" align="left">Location:</td><td width="1700" align="left" colspan="2">". $row["location"] ."</td></tr><tr><td width="200" align="left">VLAN:</td><td width="1700" align="left" colspan="2">". $row["vlan"] ."</td></tr><tr><td width="200" align="left">Service Request/Task:</td><td width="1700" align="left" colspan="2">". $row["servicerequest"] ."</td></tr>";
}
echo "</table>";
}
else {
echo "0 result";
}
$conn-> close();
?>
</table>
</body>
</html>
I am connecting my above code to a database but in an organized table fashion with css and proper table row and table dimension and table columns that I want. Below is the php code that is working(which I took idea from a youtube video) though but it does not display the data in the organized fashion that I want and it also displays ealier inputed data which I don't want, I want it to display the latest modified data for servername, contact, location, vlan, servicerequest. Here is the php databases extracting code that is working. I want to change this below code to the above code, and make the above code work.
<!DOCTYPE html>
<html>
<head>
<title>Table with database</title>
</head>
<body>
<table>
<tr>
<th>servername</th>
<th>contact</th>
<th>location</th>
<th>vlan</th>
<th>servicerequest</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "username", "password", "test");
if ($conn-> connect_error) {
die("Connection failed:". $conn-> connect_error);
}
$sql = "SELECT servername, contact, location, vlan, servicerequest from info";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
echo "<tr><td>". $row["servername"] ."</td><td>". $row["contact"] ."</td><td>". $row["location"] ."</td><td>". $row["vlan"] ."</td><td>". $row["servicerequest"] ."</td></tr>";
}
echo "</table>";
}
else {
echo "0 result";
}
$conn-> close();
?>
</table>
</body>
</html>
Once again. I want to display only the latest inputted data in the table for all 5 variables: servername, contact, location, vlan, servicerequest. And the proper table fasion. like: <tr><td>servername</td><td>$servername</td></tr><tr><td>contact</td><td>$contact</td></tr>
etc. It should only show the data inputted in the last row of the MySQL database.