0

我目前正在构建一个相当大的 php 表单,它将跨越 10 个页面,并且有超过 100 个输入。每个输入都将存储在数据库中。我只是想知道是否有更简单/更有效的方法来做到这一点。我什至没有完成第一页,而且我已经无法跟踪变量。我感谢任何提示或评论。谢谢!

我目前在做什么。想象一下这样做 x 10 -

表单示例

<form action="/db" method="post">

<div class="field-container">
    <label for="name" class="default-label">Company Name:</label>
    <input type="text" name="billing_company_name" id="name" size="50">
    <input type="hidden" name="username" value="<?php echo $current_user->user_login . "\n";?>">
</div>

<div class="field-container">
    <label for="name" class="default-label">Contact Person:</label>
    <input type="text" name="billing_contact_person" id="name" size="50">
</div>

<div class="field-container">
    <label for="name" class="default-label">Address:</label>
    <input type="text" name="billing_address" id="name" size="50">
</div>

<div class="field-container">
    <label for="name" class="default-label">City:</label>
    <input type="text" name="billing_city" id="name" size="50">
</div>

<div class="field-container">
    <label for="name" class="default-label">State:</label>
    <input type="text" name="billing_state" id="name" size="5">
</div>

<div class="field-container">
    <label for="name" class="default-label">ZIP:</label>
    <input type="text" name="billing_zip" id="name" size="8">
</div>

<div class="field-container">
    <label for="name" class="default-label">Phone:</label>
    <input type="text" name="billing_phone" id="name" size="50">
</div>

<div class="field-container">
    <label for="name" class="default-label">Email Address:</label>
    <input type="text" name="billing_email" id="name" size="50">
</div>

<input type="submit" value="Go To Step 2">
</form>

保存数据

// Insert data into mysql
$sql="INSERT INTO Forms (Form_date, username, billing_company_name,  
billing_contact_person, billing_address, billing_city, billing_state, billing_zip,  
billing_phone, billing_email)

VALUES (NOW(),'$username', '$billing_company_name', '$billing_contact_person',   
'$billing_address', '$billing_city', '$billing_state', '$billing_zip', 
'$billing_phone', 
'$billing_email' )";
$result = mysql_query($sql); 
4

3 回答 3

1

我是模板引擎的粉丝,当然你也可以用纯 PHP 来做。我建议你在一个表中插入所有需要的值,即 tb1 然后使用这个

X.php

  include '../../smarty/libs/Smarty.class.php';
  Class X extends Smarty {
  public function assignArray($query, $result_name='') {

  $data = mysql_query($query);

  $results = array();
   while ($row = mysql_fetch_assoc($data))
  $results[] = $row;

  if ($result_name != '') {
    $this->assign($result_name, $results);
    $this->assign($result_name.'_count', count($results));
  } else
  return $results;

}  
}

$x = new X();
$query = "SELECT name, size, label, flag, value, hidden
        FROM tb1 
        WHERE flag = 1";

    /*
    name                    |   size    |   label           |   flag    |   value   |   hidden      |
    -------------------------------------------------------------------------------------------------
    billing_company_name    |   50      |   Company Name    |   1       |   login   |   username    |
    billing_contact_person  |   50      |   Contact Person  |   0       |   NULL    |   NULL        |
    billing_state           |   5       |   State           |   0       |   NULL    |   NULL        |
    -------------------------------------------------------------------------------------------------
    */



$x->assignArray($query, 'result');

 //not neccessary to select flag, value, hidden

 $query1 = "SELECT name, size, label, flag, value, hidden 
        FROM tb1 
        WHERE flag = 0";
 $x->assignArray($query1, 'result1');
 $x->smarty->display('billings.tpl');

比林斯.tpl:

<form action="/db" method="post">

{section name=i loop=$result1}
<div class="field-container">
<label for="name" class="default-label">{$result1[i].label}</label>
<input type="text" name="{$result1[i].name}" id="name" size="{$result1[i].size}">
<input type="hidden" name="{$result1[i].hidden}" value="{$result1[i].value}">
</div>
{/section}

{section name=i loop=$result}
<div class="field-container">
<label for="name" class="default-label">{$result[i].label}</label>
<input type="text" name="{$result[i].name}" id="name" size="{$result[i].size}">
</div>
{/section}

<input type="submit" value="Go To Step 2">
</form>

当然,如果无法从数据库中读取 value="" 字段,您仍然可以遍历您的 currentUser 方法以获得动态值

于 2013-03-28T20:11:16.030 回答
0

您可以将表单值保存到数据库中的文本字段中,然后对 key=>values 数组进行 json_encode,这样您只有 1 个数据库数据列,并且它可以增长以容纳更多或更少的字段(json_decode 以获取数据)。然而,这样做你不能真正使用标准过滤器和来自 mysql 的排序技术直接在特定列上。您也可以两者都做,为您知道需要使用 mysql 搜索或排序的字段设置一个单独的列,并将其余部分放入 json_encoded 文本列。这可以简化数据库。至于代码和表单,通过设置配置数组并从中构建表单来执行建议(这将需要您编写脚本将配置数组转换为适当的 html/php 代码)或找到一个类他们已经可以做到这一点。

于 2013-03-28T17:13:27.843 回答
0

如果我在你的鞋子里,我会使用一个配置数组(我将在其中存储标签、字段名、输入类型、Db 列名......等等)。然后我将使用该数组来生成表单和插入查询。

(使用 Zend 或 Symfony 之类的框架执行此操作可能更简单,但我猜您不打算将框架仅用于表单)

于 2013-03-28T17:02:51.327 回答