从数据库值和列名动态创建输入字段 [英] Create input fields dynamically with values and column names from database

查看:119
本文介绍了从数据库值和列名动态创建输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用例如列中的信息,如果一个字段被称为输入 about_content 它输出关于使用输入字段内容的标签。这工作正常插入,但是我想同样使用这个code到更新,我想在数据库中输入要显示给用户一个字段的当前值。例如,如果 about_content =世界您好!我想输入的值,以反映这一点。有没有动态地这样做的方法吗?

 < PHP需要('dbc.php');mysql_select_db($分贝);
$ resultInput =的mysql_query(SHOW COLUMNS FROM哪里场NOT IN
('身份证','创造','DATE_MODIFIED','LAST_MODIFIED','更新','型','bodyText的')
和现场NOT REGEXP'_image'); //只选择我想要的列$结果= mysql_query(SELECT * FROM哪里ID =$ _ GET ['身份证']); //值我想投入为&LT值;输入>而($行= mysql_fetch_assoc($结果)及和放大器; $柱= mysql_num_rows($ resultInput)){    的foreach($行为$列=> $值){
        呼应'<标签>'$柱'<输入名称='。$列。'TYPE =输入值='$值'。>< /标签>< BR&GT ;';
    }
}
?>


解决方案

请参阅的的mysql_fetch_field

 如果(mysql_num_rows($结果)0){
//循环创建输入
//使$ resultInput对象数组。
$ I = 0;
而($行= mysql_fetch_assoc($结果)){
    $ =元的mysql_fetch_field($结果,$ I);
    如果(in_array($荟萃>的名字,$ resultInput)){
    呼应'< D​​IV CLASS =包装><标签> 。 ucfirst(str_replace函数('_','',$荟萃>名))。
    '< BR><输入名称='。$荟萃>的名字。
    '类型=文本级=输入值=$行[$荟萃>名称]>< BR>< /标签>< / DIV>';
    }
    $ I ++;
 }
}

I create inputs using the information in the columns for example if a field is called about_content it outputs a label of About Content with an input field. This works fine for inserting however I want to use this code similarly to UPDATE and I want to display to users the current value of a field as entered in the database. For example if about_content = Hello World! I want the input value to reflect that. Is there a way of doing this dynamically?

<?php

require('dbc.php');

mysql_select_db($db);
$resultInput = mysql_query("SHOW COLUMNS FROM about WHERE Field NOT IN 
('id', 'created', 'date_modified', 'last_modified', 'update', 'type', 'bodytext') 
AND Field NOT REGEXP '_image'"); // selects only the columns I want

$result = mysql_query("SELECT * FROM about WHERE id=".$_GET['id']); // values I want to put into the values for <input>

while ($row = mysql_fetch_assoc ($result) && $column = mysql_num_rows ($resultInput)) {

    foreach($row as $column => $value){
        echo '<label>'.$column.'<input name="'.$column.'" type="input" value="'.$value.'"></label><br>';
    }
}
?>

解决方案

See mysql_fetch_field.

if (mysql_num_rows($result) > 0) {
//loop creates inputs
//make $resultInput object to array.
$i=0;
while ($row = mysql_fetch_assoc($result)) {
    $meta = mysql_fetch_field($result, $i);
    if(in_array($meta->name, $resultInput )){
    echo '<div class="wrapper"><label>' . ucfirst(str_replace('_', ' ',$meta->name)) . 
    '<br><input name="' . $meta->name . 
    '" type="text" class="input" value="$row[$meta->name]"><br></label></div>';
    }
    $i++;
 }
}

这篇关于从数据库值和列名动态创建输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆