在HTML表中显示mySQL行,而不必单独指定每一列


Displaying mySQL row in an HTML table without having to specify each column individually

我正在创建一个表单,用户可以在其中查看和编辑他们的帐户信息,我希望尽可能简化它,因为有很多表单字段。我已经选择了所有客户的数据,但没有指定列名,但在HTML表中的正确位置显示时遇到了问题,而不必单独调用每个数据。

我在下面的代码中使用了mysql_fetch_array,但对于300多个字段来说,这样做会非常麻烦。我知道可以使用mysql_fetch_assoc,但我还没有弄清楚如何在正确的地方打印。

mysql_fetch_assoc (原谅我的无知,但如果不使用WHILE和FOREACH,我就无法实现这一点)

while ($row = mysql_fetch_assoc($result)) {
foreach($row as $key=>$value){    
echo "$key: $value<br />'n ";            
}}

这是有效的,但需要单独指定每个字段

<?php  
//Database connection and Open database
require_once('config.php');   
$result = mysql_query("SELECT customer_info.*, style.* FROM customer_info, style WHERE customer_info.user_id = style.user_id AND customer_info.user_id=$user_id ") or die(mysql_error());
$row = mysql_fetch_array($result );
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr><td name="fname">First Name: <?php echo $row['fname']; ?></td>
<td name="lname">Last Name: <?php echo $row['lname']; ?></td>
<td name="email">Email: <?php echo $row['email']; ?></td>
<td name="colors_love">Colors Love: <?php echo $row['colors_love']; ?></td>
</tr></table>

有没有一种方法可以从本质上逆转我用INSERT INTO代码实现这一点的方式?如果是这样的话,我假设这将简化我的下一个任务(允许用户编辑他们的信息)

$fieldlist=$vallist='';
foreach ($_POST as $key => $value) {
$fieldlist.=$key.',';
$vallist.=''''.($value).''',';
}
$result = mysql_query('INSERT INTO taste ('.$fieldlist.') VALUES ('.$vallist.')');

不考虑使用mysql库是一个糟糕的想法,您实际上已经做到了:

<table>
    <?php while ($row = mysql_fetch_assoc($result)): ?>
    <tr>
        <?php foreach($row as $key=>$value) {   
            echo "<td>$key: $value</td>'n "; 
        } ?> 
    </tr>          
    <?php endwhile; ?>
</table>

很明显,您会想为一个真正的表单清理这一点,因为回显数据库字段名并不完全是用户友好的。

这是什么?

    <?php  
//Database connection and Open database
require_once('config.php');   
$result = mysql_query("SELECT customer_info.*, style.* FROM customer_info, style WHERE customer_info.user_id = style.user_id AND customer_info.user_id=$user_id ") or die(mysql_error());
$row = mysql_fetch_assoc($result );
?>
<table border="0" cellspacing="2" cellpadding="2">
    <tr><?php foreach($row as $field => $content): ?>
        <td><?php echo $field; ?>: <?php echo $content; ?></td>
    <?php endforeach; ?></tr>
</table>