Kevin Lynn
Kevin Lynn

Reputation: 11

Select function on sql table

I asked a question about this issue - adding a drop down list to a table. Everyone focused in on a <td> href I had in the table, which was actually working, despite what I was told was obsolete syntax.

So I thought ok well they know more than me... so at first I tried the suggestions which didn't work. Then I thought well why not just pull the thing altogether? maybe it's causing problems with the rest of the code.

So I removed it completely, so it's definitely not that - It's this <td><select> element that's causing the issue. Yet that same element works on it's own... and the rest of my code works without it???

My experience in this is days old guys, please just a small assist would mean a lot

<!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", "Me", "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>
                       <select>        
                            <option value="volvo">Volvo</option>
                            <option value="saab">Saab</option>
                            <option value="mercedes">Mercedes</option>
                            <option value="audi">Audi</option>
                       </select>
                    </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>

Upvotes: 1

Views: 28

Answers (2)

Kevin Lynn
Kevin Lynn

Reputation: 11

Dino you're a legend. Hours I sat with that... sigh. Anywho it's sorted now and for anyone following behind me many months from now. This was the solution. I might ajust the other quotes as suggested Dino for now I'm glad it's working

echo
"<tr>
                    <td>" . $row["id"]. "</td>                  
                    <td>" . $row["Location"] . "</td>
                    <td>" . $row["Notes"] . "</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> 
                </tr>";}

Upvotes: 0

DinoCoderSaurus
DinoCoderSaurus

Reputation: 6520

It's the quotes.

This is invalid PHP and will (should?) give a PHP error (if display_errors is On in php.ini file):

echo
"<tr>
                    <td>" . $row["id"]. "</td>                  
                    <td>" . $row["Location"] . "</td>
                    <td>" . $row["Notes"] . "</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> 

                </tr>";

IMO you want the value attributes in double-quotes, so suggest changing all the other quotes to single-quote/tick (')

Upvotes: 1

Related Questions