我需要在表格单元格中添加一个下拉列表

时间:2016-12-30 15:00:01

标签: php

我需要将两个下拉列表(从Mysql查询中)添加到表中。我有一张表显示了r aquery的结果,然后我需要让两列的单元格中有下拉列表。这将是一个最终将创建文件的表单。

到目前为止,这是我的代码:

    <?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); 
?> 

我试图将select插入到行中,但页面没有加载我收到服务器错误。

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

好的,试试这个:

<?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>";
?>

如果您收到任何错误,请将它们放在此处。请记住,上面的代码是不好的,你不应该这样编码。了解事情如何运作是一个良好的开端,但是稍后你应该重构它。写得更好,更具可读性,编写自己的函数从db获取数据,使用mysqli函数代替mysql甚至是像PDO这样的库,将逻辑与视图分开等。