1

I tried to display data from the MySQL database into an HTML table using PHP, everything works fine except for "Cars requested" it shows above the table not inside the table

enter image description here

This is my code:

<!DOCTYPE html>
<html lang="en">
<head>

<style>
table, th, td {
    border: 1px solid black;
}
</style>
    <!-- <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
    <title>tables</title>
</head>
<body>
    <table>
    
    <tr>
    <th>Customer name</th>
    <th>Customer phone</th>
    <th>Age</th>
    <th>Cars requested</th>
    <?php
    $servername = "localhost";
    $username = "root"; // For MYSQL the predifined username is root
    $password = ""; // For MYSQL the predifined password is " "(blank)
    $db = "car";
    
// Create connection
$conn = new mysqli($servername, $username, $password,$db);
if ($conn->connect_error)  {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT fname, phone, age, test from info";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["fname"]. "</td> <td>" . $row["phone"] . "</td><td>" . $row["age"] . "</td></td>" .$row["test"] . "</td></tr>";
    }
    echo "</table>";
}

else
{
    echo "0 result";
}
$conn->close();
?>
    </tr>
    </table>
</body>
</html>

How can I fix this error?

2
  • Cars requested is a checkbox Commented Oct 25, 2020 at 20:00
  • 3
    "</td></td>" before .$row["test"] should be "</td><td>" Commented Oct 25, 2020 at 20:07

2 Answers 2

2

The print code echo should look like this:

echo "<tr><td>" . $row["fname"]. "</td> <td>" . $row["phone"] . "</td><td>" . 
$row["age"] . "</td><td>" .$row["test"] . "</td></tr>";

The table structure was not closed.

Sign up to request clarification or add additional context in comments.

Comments

1
 "</td></td>" .$row["test"] 

This is the issue I guess. It should be <td></td> and not </td></td>.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.