Php 根据数据库字段创建HTML表单

Php 根据数据库字段创建HTML表单,php,jquery,html,mysql,fat-free-framework,Php,Jquery,Html,Mysql,Fat Free Framework,我正在尝试创建一个程序,该程序将读取数据库表的结构,并根据它生成一个简单的HTML注册表单。如果该表如下所示: TBLUSERS - Name (varchar) - Age (int) - Birthday (date) <form id="generated-form"> Name: <input type="text" id="name"><br> Age: <input type="text" id="age"><br&

我正在尝试创建一个程序,该程序将读取数据库表的结构,并根据它生成一个简单的HTML注册表单。如果该表如下所示:

TBLUSERS
- Name (varchar)
- Age (int)
- Birthday (date)
<form id="generated-form">
   Name: <input type="text" id="name"><br>
   Age: <input type="text" id="age"><br>
   Birthday: <input type="date" id="birthday"><br>
   <input type="submit">
</form>
然后,它将输出如下表单:

TBLUSERS
- Name (varchar)
- Age (int)
- Birthday (date)
<form id="generated-form">
   Name: <input type="text" id="name"><br>
   Age: <input type="text" id="age"><br>
   Birthday: <input type="date" id="birthday"><br>
   <input type="submit">
</form>

名称:
年龄:
生日:
但我不知道从哪里开始。我更喜欢使用任何包含FatfreeFramework的内容,因为大多数项目都是使用它完成的。除此之外,我只应该使用HTML、JQuery、PHP和/或Javascript


有什么建议吗?提前谢谢

当然,第一步是通过使用获取表的列信息

然后,您可以像任何普通查询一样查询它,在本例中使用
mysqli

例如:

<?php

$db = new mysqli('localhost', 'username', 'password', 'database');
$query = $db->query('DESCRIBE `TBLUSERS`');
$fields = array();
while($row = $query->fetch_assoc()) {
    $fields[] = $row['Field'];
}

?>

<form id="generate-form" type="POST">
    <?php foreach($fields as $field): ?>
        <label>
            <?php echo "$field: "; ?>
            <input type="text" name="<?php echo $field; ?>" />
        </label><br/>
    <?php endforeach; ?>
    <input type="submit" name="submit" />
</form>


用这样的东西

 <?php

$result = mysql_query ("SHOW COLUMNS FROM sometable" );
while ( $row = mysql_fetch_assoc($result )) {
#$row contains a 3D-array of fields with their flags like
#Array
#(
#[Field] => name
#[Type] => varchar(7)
#[Null] =>
#[Key] => PRI
#[Default] =>
#[Extra] => auto_increment
#)

 #generate form like this
foreach($row as $column){
if($column[Type] == varchar(' /^[0-9]+$/i') $type= "text";
 elseif($other-types) $type = /*html-type-match*/;
 echo ucwords($column[Field])."<input type=\"$type\" />
}
?>

根据请求使用重影示例,包括F3及其模板引擎

<?php

$f3 = require 'lib/base.php';

$db = new DB\SQL('mysql:host=localhost;dbname=DBNAME', 'USERNAME', 'PASSWORD');
$res = $db->exec('DESCRIBE `TBLUSERS`');

foreach($res as $row) {
    $fields[] = $row['Field'];
}

$f3->set('fields', $fields);

echo Template::instance()->render('form.html');

?>

<!-- form.html -->
<form id="generate-form" type="POST">
    <repeat group="{{@fields}}" value="{{@field}}">
        <label>
            {{@field}}:
            <input type="text" name="{{@field}}" />
        </label><br/>
    </repeat>
    <input type="submit" name="submit" />
</form>

{{@field}}:


在F3中更容易使用,而且多引擎兼容:

$db = new DB\SQL('mysql:host=localhost;dbname=DBNAME', 'USERNAME', 'PASSWORD');
$schema = $db->schema('TBLUSERS');
$f3->set('fields', array_keys($schema));
还要检查模式的完整返回值:

也可能在一行中

$f3->set('fields', array_keys($db->schema('items')));

快速,简单,正是我想要的。非常感谢你,鬼D@user2176205当然很高兴helped@Ghost这非常有帮助,但假设我有选择字段,即下拉列表,那么我们可以怎么做that@Sanj您需要在foreach,if文本字段中指定if条件,然后使用上面的,如果该字段是select,那么这样做,只需创建一个简单的select下拉列表,概念相同,只是一个不同的htmlmarkup@Ghost感谢我这边对你的回复+1。我在F3中实现了这一点,这确实是一个很大的帮助,但我是一个F3新手,似乎无法做到这一点。它说“为foreach()提供的参数无效”。要么是您使用我的示例错误,要么是我在DB查询中出错,它没有返回任何内容。只要试着
var\u dump($res)
看看里面有什么。我建议您使用@ikkez方法。我刚刚忘记了schema(),感谢表单生成验证!帮了大忙丹麦时间,用户2176205。