我有一个表格可以选择并回显列(th)和字段数据(td)的名称。但用户可以添加和删除列。我如何编写更适应用户更改的更灵活的代码?我的意思是能够在不知道所有字段的情况下拥有整个表格。选择并回显所有表格(不管它是什么)
<?php
$sql = "SELECT * from eee";
$result = mysql_query($sql,$con);
$id = mysql_field_name($result, 0);
$a = mysql_field_name($result, 1);
$b = mysql_field_name($result, 2);
$c = mysql_field_name($result, 3);
?>
<tr>
<th><input class="gris" type="text" name="<?php echo $id ?>" value="<?php echo $id ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $a ?>" value="<?php echo $a ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $b ?>" value="<?php echo $b ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $c ?>" value="<?php echo $c ?>"/></th>
</tr>
<?php
$result = mysql_query("SELECT * FROM eee");
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td> <input class="blanc" type="text" name="num" value="<?php echo $row['id']?>"/> </td>
<td><input class="blanc" type="text" name="a" value="<?php echo $row['a']?>"/></td>
<td><input class="blanc" type="text" name="b" value="<?php echo $row['b']?>"/></td>
<td><input class="blanc" type="text" name="c" value="<?php echo $row['c']?>"/></td>
</tr>
<?php } ?>
</table>
[**请注意,不要在新代码中使用'mysql_ *'函数**](http://bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 – PeeHaa
首先,请参阅http://php.net/manual/en/function.mysql-field-name.php。 mysql_ *已被弃用。其次,回答你的问题:你可以简单地遍历字段名:'for($ i = 0,$ field_name = mysql_field_name($ result,$ i);($ field_name = mysql_field_name($ result,$ i))!= = false; ++ $ i){用field_name ...做些什么}' – davak