Displaying MySQL data using PHP I have MySQL data that I am rendering in an HTML table using PHP. This is the full code of my table and PHP. It does output fine and looks how I would expect but I am wondering if there is a better way to write this code. Also, the 'Delete' button specifically, is there a better way to output the button itself as right now, I noticed especially on a mobile device, I will see a text cursor indicator when tapping the button as if I am trying to select the text on the button itself.

&lt;div class="container" style="width: 95%;"&gt;
    &lt;table class="table table-bordered table-striped table-hover"&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th class="col-sm-6"&gt;
                    Description
                &lt;/th&gt;
                &lt;th class="col-sm-2"&gt;
                    Date Created
                &lt;/th&gt;
                &lt;th class="col-sm-1"&gt;
                    Created By
                &lt;/th&gt;
                &lt;th class="col-sm-2" style="text-align: right;"&gt;
                    Edit - Delete
                &lt;/th&gt;
            &lt;/tr&gt;
        &lt;?php

        $noLists = "&lt;div&gt;You have no lists. Why dont you create one?&lt;/div&gt;";

        if ($db-&gt;connect_error) {
            die("Connection failed: " . $db-&gt;connect_error);
        }

        $sql = "SELECT DISTINCT lists.id, lists.listdescription, lists.createdon, lists.createdby, lists.sharedlist FROM lists INNER JOIN users ON users.fname = '$displayname' AND users.fname = lists.createdby";
        $result = $db-&gt;query($sql);

        if ($result-&gt;num_rows &gt; 0) {
            // output data of each row
            while($row = $result-&gt;fetch_assoc()) {

                $listID = $row["id"];
                $listDescription = $row["listdescription"];
                $createdOn = $row["createdon"];
                $createdBy = $row["createdby"];

                echo "&lt;form&gt;";
                echo "&lt;tr&gt;";
                echo "&lt;td&gt;$listDescription&lt;/td&gt;";
                echo "&lt;td&gt;$createdOn&lt;/td&gt;";
                echo "&lt;td&gt;$createdBy&lt;/td&gt;";
                echo "&lt;td align=\"right\"&gt;";
                echo "&lt;form action='account.php' method='post'&gt;";
                echo "&lt;input class=\"btn btn-success\" type=\"submit\" name=\"\" value=\"Edit\"&gt;";

                echo "&lt;a href='includes/deletelist.php?id=$row[id]'&gt;&lt;input class=\"btn btn-danger\" value=Delete style=\"width: 85px;\"&gt;&lt;/a&gt;
                &lt;!--&lt;a href=\"index.php?id=$id\"&gt;Delete&lt;/a&gt;--&gt;
                &lt;/td&gt;";


            }
        } else {
            echo "&lt;/thead&gt;";
            echo "&lt;tbody&gt;";

            echo "&lt;/tbody&gt;";
            echo "&lt;/table&gt;";
            echo "&lt;h4&gt; $noLists&lt;/h4&gt;";
        }
        $db-&gt;close();

        ?&gt;
Generally speaking, you should keep your markup and your PHP logic separate.

And the intermediate variables don't really have a purpose, just use the values from the array.

You've got two opening form tags and no closing form tags.

Your first condition (i there are records in the table) does not close the thead or the create a tbody.

Your delete button is no button at all, it's an input wrapped in an anchor tag (&lt;a&gt;). That's why it's not working as expected. Either remove the anchor and give the input a type attribute of "button", or remove the input and put some text in there instead.

Use a prepared statement. It's not cool to throw variables into your queries all willy-nilly. You will anger the code gods.

Here's a quick re-write (assumes you're using PDO - If you're using MySQLi, see the docs to convert the prepared statement to MySQLi)

I'm just going to go ahead and post this since i spend three or four minutes on it, but after looking at this code, there is no way this was working as you had it. This site is for reviewing working code, not fixing broken code. If your code doesn't work you need to ask on StackOverflow in the future.

&lt;?php

    // Do all the PHP stuff before you start ouputting
    $noLists = "&lt;div&gt;You have no lists. Why dont you create one?&lt;/div&gt;";

    if ($db-&gt;connect_error) {
        die("Connection failed: " . $db-&gt;connect_error);
    }

    $sql = "SELECT DISTINCT lists.id, lists.listdescription, lists.createdon, lists.createdby, lists.sharedlist FROM lists INNER JOIN users ON users.fname = ? AND users.fname = lists.createdby";
    $result = $db-&gt;prepare($sql);
    $result-&gt;execute(array($displayname));
    $output = array();
    if ($result-&gt;rowCount() &gt; 0) {
        // output data of each row

        $output[] = "&lt;/thead&gt;";
        $output[] = "&lt;tbody&gt;";

        while($row = $result-&gt;fetch(PDO::FETCH_ASSOC)) {

            $output[] = "&lt;tr&gt;";
            $output[] = "&lt;td&gt;{$row["listdescription"]}&lt;/td&gt;";
            $output[] = "&lt;td&gt;{$row["createdon"]}&lt;/td&gt;";
            $output[] = "&lt;td&gt;{$createdBy}&lt;/td&gt;";
            $output[] = "&lt;td align=\"right\"&gt;";
            $output[] = "&lt;form action='account.php' method='post'&gt;";
            $output[] = "&lt;input class=\"btn btn-success\" type=\"submit\" name=\"\" value=\"Edit\"&gt;";
            $output[] = "&lt;/form&gt;";
            $output[] = "&lt;a href='includes/deletelist.php?id={$row["id"]}' class=\"btn btn-danger\" style=\"width: 85px;\"&gt;Delete&lt;/a&gt;&lt;/td&gt;";
        }

        $output[] = "&lt;/tbody&gt;";
        $output[] = "&lt;/table&gt;";

    } else {
        $output[] = "&lt;/thead&gt;";
        $output[] = "&lt;tbody&gt;";
        $output[] = "&lt;/tbody&gt;";
        $output[] = "&lt;/table&gt;";
        $output[] = "&lt;h4&gt; $noLists&lt;/h4&gt;";
    }
    $db-&gt;close();

    // Turn the output array back into a string
    $output = implode('', $output);

?&gt;&lt;div class="container" style="width: 95%;"&gt;
    &lt;table class="table table-bordered table-striped table-hover"&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th class="col-sm-6"&gt;
                    Description
                &lt;/th&gt;
                &lt;th class="col-sm-2"&gt;
                    Date Created
                &lt;/th&gt;
                &lt;th class="col-sm-1"&gt;
                    Created By
                &lt;/th&gt;
                &lt;th class="col-sm-2" style="text-align: right;"&gt;
                    Edit - Delete
                &lt;/th&gt;
            &lt;/tr&gt;
        &lt;?php echo $output; ?&gt;


Edit... you're not closing your table row in the loop either. I'm not gonna bother updating the code snippet.