0

Hi guys I have a working simplified php table as shown below.

<!DOCTYPE html>
<html>
<head>
    <title>LifeSaver DB</title>
    <h1> LifeSaver Database </h1>
</head>
<body>
    <table>
        <tr>
            <th>Id</th>
            <th>Location</th>
            <th>Footage</th>
            <th>Notes</th>

        </tr>
        <?php
        $conn = mysqli_connect("localhost", "Kevin", "Pass", "LifeSaverDB"); 
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            }

        $sql = "SELECT id, Location, Footage, Notes FROM LifeSaver1";
        $result = $conn->query($sql);

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

                //for href row
                $id = $row['id'];
                $Footage = ['Footage'];

                echo 
                "<tr>
                    <td>" . $row["id"]. "</td>

                    <td>" . $row["Location"] . "</td>
                    <td>" . $row["Notes"] . "</td>
                    <td> <a href='test.php'?id= . $id .'>" . $row['Footage'] . "</a> </td>

                </tr>";}

                //show table
                echo "</table>";
                } else { echo "0 results"; }
            $conn->close();

    ?>
    </table>
</body>
<style>

table, td, th {
  border: 1px solid black;
  margin: auto;
}

table {
  border-collapse: collapse;
color: #000;   <!--font colour -->
font-family: monospace;
font-size: 18px;
text-align: center;}

th {
background-color: #337AFF;
color: white;
font-weight: bold; }

tr:nth-child(odd) {background-color: #add8e6}


</style>
</html>

I wanted to add a drop down menu column, now I hacked apart this little element from a larger script, that I thought would do the job.

<html>

<TABLE>


   <tr>  
    <td>
       <select>        
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
            <option value="mercedes">Mercedes</option>
            <option value="audi">Audi</option>
       </select>
    </td>        
</tr>
</TABLE>

</html> 

So I thought I could just sandwich it right in there as shown below. But it says the server can't handle the request. I think the while loop doesn't like the fact that it's just a standalone list and not a row request from the phpmyadmin, but I could be wrong. Please help

<!DOCTYPE html>
<html>
<head>
    <title>LifeSaver DB</title>
    <h1> LifeSaver Database </h1>
</head>
<body>
    <table>
        <tr>
            <th>Id</th>
            <th>Drop</th>
            <th>Location</th>
            <th>Footage</th>
            <th>Notes</th>          
        </tr>
        <?php
        $conn = mysqli_connect("localhost", "Kev", "Pass", "LifeSaverDB"); 
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            }

        $sql = "SELECT id, Location, Footage, Notes FROM LifeSaver1";
        $result = $conn->query($sql);

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

                //for href row
                $id = $row['id'];
                $Footage = ['Footage'];

                echo 
                "<tr>
                    <td>" . $row["id"]. "</td>
                    <td>
                       <select>        
                            <option value="volvo">Volvo</option>
                            <option value="saab">Saab</option>
                            <option value="mercedes">Mercedes</option>
                            <option value="audi">Audi</option>
                       </select>
                    </td> 
                    <td>" . $row["Location"] . "</td>
                    <td>" . $row["Notes"] . "</td>
                    <td> <a href='test.php'?id= . $id .'>" . $row['Footage'] . "</a> </td>

                </tr>";}

                //show table
                echo "</table>";
                } else { echo "0 results"; }
            $conn->close();

    ?>
    </table>
</body>
<style>

table, td, th {
  border: 1px solid black;
  margin: auto;
}

table {
  border-collapse: collapse;
color: #000;   <!--font colour -->
font-family: monospace;
font-size: 18px;
text-align: center;}

th {
background-color: #337AFF;
color: white;
font-weight: bold; }

tr:nth-child(odd) {background-color: #add8e6}


</style>
</html>

Kevin Lynn
  • 11
  • 3
  • 1
    You should be getting parse errors when enabling error reporting. You need to escape the double quotes when inside an encapsulated echo in double quotes. – Funk Forty Niner Jan 22 '20 at 21:17
  • Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is largely an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. You're using both styles here inconsistently. – tadman Jan 22 '20 at 21:26
  • Guys I'll be perfectly honest here. My knowledge of this is a few days old so I don't even know how to consider what's obsolete or not. I'm an EEE student swimming way out of my depth here. The Href is probably is a mess but it was working. Adding the – Kevin Lynn Jan 22 '20 at 21:39
  • I changed my working code to this one as recommended by @tadman. ``` while($row = $result->fetch_assoc()) { //for href row $id = $row['id']; $Footage = ['Footage']; echo " " . $row["id"]. " " . $row["Location"] . " " . $row["Notes"] . " "" . $row['Footage'] . " ";} //show table echo ""; } else { echo "0 results"; } $conn->close(); ``` But it's not working – Kevin Lynn Jan 22 '20 at 21:59
  • If you have an update to your question it's a lot easier to edit it and include code. In comments it's really unreadable. – tadman Jan 23 '20 at 00:51
  • If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. Everything you're doing is a solved problem. – tadman Jan 23 '20 at 00:51

0 Answers0