-1

I need to add two dropdown lists (from a Mysql query) to a table. I have the table that shows the results of r aquery and then I need to have the cells of the two columns with the dropdowns in them. This is going to be a form that will eventually create files.

Here is my code so far:

    <?php

$link = mysql_connect("localhost", "", "") or die ('Error connecting to mysql' . mysql_error());

mysql_select_db("cqadmin");  

$sql = "SELECT id , mac FROM phones order by mac;"; 
$result = mysql_query($sql) or die(mysql_error());
$sql1 = "SELECT id , templatename FROM templates order by templatename;";
$result1 = mysql_query($sql1) or die(mysql_error());
$sql2 = "SELECT extension, secret from extensions;";
$result2 = mysql_query($sql2) or die(mysql_error());
echo "<table border='3'>
<tr>
<th>Extension #</th>
<th>Secret</th>
<th>MAC Address</th>
<th>Template</th>

</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['extension'] . "</td>";
echo "<td>" . $row['secret'] . "</td>";
echo "<td>" . $row[''] . "</td>";
echo "<td>" . $row[''] . "</td>";
echo "</tr>";
}
echo "</table>";

?>
<p>  
    <select name="phone">  
    <?php
    while($row = mysql_fetch_array($result)) { 
        echo '<option value="' . $row['id'] . '">' . $row['mac']  . '</option>';
    }  
    ?>  
    </select>
<select name="template">
    <?php
    while($row = mysql_fetch_array($result1)) {
        echo '<option value="' . $row['id'] . '">' . $row['templatename']  . '</option>';
    }
    ?>
    </select>

</p>
<?php
mysql_close($link); 
?> 

I have tried to insert the select into the row but the page doesn't load I get an server error.

Any help is much appreciated

Ed Lentz
  • 7
  • 3

1 Answers1

0

Ok, try this:

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

$link = mysql_connect("localhost", "", "") or die ('Error connecting to mysql' . mysql_error());

mysql_select_db("cqadmin");  

$sql2 = "SELECT extension, secret from extensions;";
$result2 = mysql_query($sql2) or die(mysql_error());
echo "<table border='3'>
    <tr>
        <th>Extension #</th>
        <th>Secret</th>
        <th>MAC Address</th>
        <th>Template</th>
    </tr>";

while($row = mysql_fetch_array($result2))
{
    $sql = "SELECT id , mac FROM phones order by mac;"; 
    $result = mysql_query($sql) or die(mysql_error());
    $sql1 = "SELECT id , templatename FROM templates order by templatename;";
    $result1 = mysql_query($sql1) or die(mysql_error());
    echo "<tr>";
    echo "<td>" . $row['extension'] . "</td>";
    echo "<td>" . $row['secret'] . "</td>";
    echo "<td> <select name='phone'>";
    while($rowA = mysql_fetch_array($result)) { 
        echo '<option value="' . $rowA['id'] . '">' . $rowA['mac'] . '</option>';
    }  
    echo "</select></td>";
    echo "<td><select name='template'>";
    while($rowB = mysql_fetch_array($result1)) {
        echo '<option value="' . $rowB['id'] . '">' . $rowB['templatename'] . '</option>';
    }
    echo "</select></td>";
    echo "</tr>";
}
echo "</table>";
?>

If you get any errors put them here. Keep in mind that the code above is preety bad, you shouldn't code like that. It's a good start to learn how things work, but later you should refactor it. Write it better, more readable, write your own functions getting data from db, use mysqli functions instead mysql or even library like PDO, separate your logic from the view etc.

Raduch 87
  • 16
  • 1
  • Raduch, I tried your suggestion and I get a error 500 – Ed Lentz Dec 30 '16 at 18:21
  • Ok I tried to add Raduch's code again and I am getting closer to the results I want. I get the fully populated dropdown for both selects in the first row, the rest of the rows has a blank dropdown. – Ed Lentz Dec 30 '16 at 18:30
  • It occurs to me that the initial table length is determined by result2 Is that why the result and result1 are only 1 row? – Ed Lentz Dec 30 '16 at 18:34
  • Hey, sorry i was out of office. Try this: – Raduch 87 Dec 31 '16 at 17:18
  • Hey, sorry i was out of office. Try this: while($row = mysql_fetch_array($result2)) { $result = mysql_query($sql) or die(mysql_error()); $result1 = mysql_query($sql1) or die(mysql_error()); /* rest of the code */ } – Raduch 87 Dec 31 '16 at 17:28
  • I am not used to this commenting system... Anyway, I tried replacing the code: while($rowA = mysql_fetch_array($result)) { echo ''; with your updated code and I got a blank screen. – Ed Lentz Dec 31 '16 at 18:11
  • Hey Ed, i've eddited the anwser above, code there is more readable. – Raduch 87 Jan 01 '17 at 13:08
  • Hey Raduch that works! Thank you so much! I will probably refactor all of this project once I get it working in a crude sense. Now to take that information and create text files out of it. Thank you again for all your help. Happy New Year! – Ed Lentz Jan 01 '17 at 13:56