我的代码不会从MySQL表中呈现PHP

I've been working with this one page forever using MAMP and I can't get the PHP to render. It must be something with getting the info from the table but I can't figure out what. I'm pretty sure I have everything lined up correctly.

For a while it was just sending me back jibberish and then nothing but eventually I got it render the HTML through localhost by switching the while loop to a do-while loop so that it would run through once. So the issue must be with the connection to MySQL I'm thinking, but I don't know what.

<?php 

$product_id = $_GET['id'];
$link = mysqli_connect('localhost', 'root', 'root', 'legend');
$result = mysqli_query($link, "SELECT * FROM test WHERE id=" . $product_id . "'");
while($row = mysqli_fetch_array($result)) {
?>

<html>
<head>
</head>
<body>
    <div>
        <p>Name: <?php echo $row['unitOne']; ?></p>
        <p>Box Quantity: <?php echo $row['unitTwo']; ?></p>
    </div>
</body>
</html>

<?php
}

mysqli_close($link);
?>

Try to structure your code this way and please consider all the comments below your first post. I also recommend reading this article that will clarify what prepared statements and bound parameters are: http://www.w3schools.com/php/php_mysql_prepared_statements.asp

Remember to always construct your HTML markup correctly. There should never be more than one open and close tag for html, head and body. And if you do have more that can surely mess things up for you.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cart</title>
</head>
<body>
<?php
$query = "SELECT * FROM products WHERE product_id = ?"; //for security we will be using prepared statements
$stmt = $link -> mysqli -> prepare($query); //$link contains your database connection
$stmt -> bind_param('i', $id); //$id contains a product id the "i" will define it as an integer
$stmt -> execute();

$result = $stmt -> fetch_result();
while($row = $result -> fetch_assoc()){ //loop out the results
    echo '<div>';
        echo '<p>Name: '.$row['unitOne'].'</p>';
        echo '<p>Box Quantity: '.$row['unitTwo'].'</p>';
     echo '</div>';
    }
?>
</body>
</html>