使用用户输入插入表格

时间:2014-11-05 10:19:35

标签: php html mysql

这可能有一个简单的解决方案,但我在这里难倒。如您所见,我希望员工能够在一个表单中请求多达6个不同的项目。对我来说问题是,如何在各自的列中存储6个可能的值(项目编号,Ited描述,数量请求和请求目的)?我可以使用数组,但这会让它更复杂。

这是我的表格描述:

+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| Request_Number     | int(11)      | NO   | PRI | NULL    | auto_increment |
| Request_Date       | date         | NO   |     | NULL    |                |
| Department         | varchar(30)  | NO   |     | NULL    |                |
| Email              | varchar(80)  | NO   |     | NULL    |                |
| Item_Number        | int(11)      | YES  |     | NULL    |                |
| Item_Description   | varchar(250) | YES  |     | NULL    |                |
| Quantity_Requested | varchar(20)  | YES  |     | NULL    |                |
| Request_Purpose    | varchar(255) | YES  |     | NULL    |                |
| Sub_Dept_Head      | varchar(50)  | YES  |     | NULL    |                |
| Dept_Head          | varchar(50)  | YES  |     | NULL    |                |
| Admin_Dept         | varchar(50)  | YES  |     | NULL    |                |
| Approved_By        | varchar(50)  | YES  |     | NULL    |                |
+--------------------+--------------+------+-----+---------+----------------+

这是表格:

    <form class="Requisition_form" id="form1" name="form1" method="post" action="">
      <table width="100%" border="2" cellspacing="0" cellpadding="5">
        <tr>
          <th colspan="7">Personal Detail:</th>
        </tr>
        <tr>
          <td width="155"><strong>Request Date:</strong></td>
          <td width="199"><label>
            <input size="30" type="date" name="date" id="date" placeholder="yyyy/mm/dd"/>
          </label></td>
          <td width="133"><strong>Department</strong></td>
          <td width="182"><label>
            <input size="30" type="text" name="department" id="department" />
          </label></td>
        </tr>
        <tr>
          <td><strong>Requested By:</strong></td>
          <td><label>
            <input size="30" type="text" name="requested_by" id="requested_by" />
          </label></td>
          <td><strong>E-Mail:</strong></td>
          <td><label>
            <input size="30" type="email" name="email" id="email" />
          </label></td>
        </tr>
      </table>
      <table width="100%" border="2" cellspacing="0" cellpadding="5">
    <tr>
      <th colspan="4">Requisition Detail:</th>
    </tr>
    <tr>
      <td width="25%" align="center"><strong>Item Number</strong></td>
      <td width="25%" align="center"><strong>Item Description</strong></td>
      <td width="25%" align="center"><strong>Quantity Requested</strong></td>
      <td width="25%" align="center"><strong>Request Purpose</strong></td>
    </tr>
    <tr>
      <td align="center"><label>
        <input size="30" type="text" name="item_number" id="item_number" />
      </label></td>
      <td align="center"><label>
        <input size="30" type="text" name="item_description" id="item_description" />
      </label></td>
      <td align="center"><label>
        <input size="30" type="text" name="qty_requested" id="qty_requested" />
      </label></td>
      <td align="center"><label>
        <input size="30" type="text" name="request_purpose" id="request_purpose" />
        </label></td>
    </tr>
    <tr>
      <td align="center"><input size="30" type="text" name="item_number2" id="item_number2" /></td>
      <td align="center"><input size="30" type="text" name="item_description2" id="item_description2" /></td>
      <td align="center"><input size="30" type="text" name="qty_requested2" id="qty_requested2" /></td>
      <td align="center"><input size="30" type="text" name="request_purpose2" id="request_purpose2" /></td>
    </tr>
    <tr>
      <td align="center"><input size="30" type="text" name="item_number3" id="item_number3" /></td>
      <td align="center"><input size="30" type="text" name="item_description3" id="item_description3" /></td>
      <td align="center"><input size="30" type="text" name="qty_requested3" id="qty_requested3" /></td>
      <td align="center"><input size="30" type="text" name="request_purpose3" id="request_purpose3" /></td>
    </tr>
    <tr>
      <td align="center"><input size="30" type="text" name="item_number4" id="item_number4" /></td>
      <td align="center"><input size="30" type="text" name="item_description4" id="item_description4" /></td>
      <td align="center"><input size="30" type="text" name="qty_requested4" id="qty_requested4" /></td>
      <td align="center"><input size="30" type="text" name="request_purpose4" id="request_purpose4" /></td>
    </tr>
    <tr>
      <td align="center"><input size="30" type="text" name="item_number5" id="item_number5" /></td>
      <td align="center"><input size="30" type="text" name="item_description5" id="item_description5" /></td>
      <td align="center"><input size="30" type="text" name="qty_requested5" id="qty_requested5" /></td>
      <td align="center"><input size="30" type="text" name="request_purpose5" id="request_purpose5" /></td>
    </tr>
    <tr>
      <td align="center"><input size="30" type="text" name="item_number6" id="item_number6" /></td>
      <td align="center"><input size="30" type="text" name="item_description6" id="item_description6" /></td>
      <td align="center"><input size="30" type="text" name="qty_requested6" id="qty_requested6" /></td>
      <td align="center"><input size="30" type="text" name="request_purpose6" id="request_purpose6" /></td>
    </tr>
  </table>

      <table width="100%" border="2" cellspacing="0" cellpadding="5">
        <tr>
          <th colspan="4">Recommendations:</th>
        </tr>
        <tr>
          <td align="center"><strong>Sub-Department Head</strong></td>
          <td align="center"><strong>Department Head</strong></td>
          <td align="center"><strong>Administration</strong></td>
          <td align="center"><strong>Approved by</strong></td>
        </tr>
        <tr>
          <td align="center"><input size="30" type="text" name="sub_dept_head" id="sub_dept_head" /></td>
          <td align="center"><input size="30" type="text" name="dept_head" id="dept_head" /></td>
          <td align="center"><input size="30" type="text" name="admin_dept" id="admin_dept" /></td>
          <td align="center"><input size="30" type="text" name="approved_by" id="approved_by" /></td>
        </tr>
      </table>
    </form>

1 个答案:

答案 0 :(得分:1)

您应该使用数组来捕获输入值。您需要使用以下输入更改每一行:

 <tr>
      <td align="center"><label>
        <input size="30" type="text" name="item_number[]" id="item_number" />
      </label></td>
      <td align="center"><label>
        <input size="30" type="text" name="item_description[]" id="item_description" />
      </label></td>
      <td align="center"><label>
        <input size="30" type="text" name="qty_requested[]" id="qty_requested" />
      </label></td>
      <td align="center"><label>
        <input size="30" type="text" name="request_purpose[]" id="request_purpose" />
        </label></td>
    </tr>

现在在PHP中,您可以简单地遍历并获取每行的每个项目。

$c = count($_POST['item_number']);
for ($i = 0; $i < $c; $i++) {
    $item_number = $_POST['item_number'][$i];
    $item_description= $_POST['item_description'][$i];
    $qty_requested= $_POST['qty_requested'][$i];
    $request_purpose= $_POST['request_purpose'][$i];

    // Enter to database or whatever
}

但是这仍然不起作用,因为您的数据库设置不正确。您需要为项目设置单独的表,然后与请求建立一对多的关系。

如果由于某种原因无法更改数据库结构,则需要序列化每个数组,然后将序列化字符串输入数据库。例如:

$item_number = serialize($_POST['item_number']);
$item_description= serialize($_POST['item_description']);
$qty_requested= serialize($_POST['qty_requested']);
$request_purpose= serialize($_POST['request_purpose']);

希望有所帮助:)