PHP 订单捕获门户 [英] PHP Order Capture Portal

查看:33
本文介绍了PHP 订单捕获门户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始之前我需要一些建议.我的 PHP 技能只有 3 个月大,所以我边走边学.

I need some advice before I start. My PHP Skills are only 3 months old so learning as I go.

我需要创建一个订单捕获屏幕,将信息捕获到数据库中.

I need to create an order capture screen that captures the information to a database.

订单最多可以有 150 行,需要全部捕获.

The order form can have up to 150 lines that all need to be captured.

我可以很容易地创建表单,但我想知道将其写入数据库的最佳方法是什么.

I can create the form easy enough but am wondering what the best way to write this to the database is.

提交时,我可以使用 post 方法获取表单值并使用以下方法将它们写入 mysql:

on submitting, I can get the form values using the post method and write them to mysql using:

INSERT INTO example
  (example_id, name, value, other_value)
VALUES
  (100, 'Name 1', 'Value 1', 'Other 1'),
  (101, 'Name 2', 'Value 2', 'Other 2'),
  (102, 'Name 3', 'Value 3', 'Other 3'),
  (103, 'Name 4', 'Value 4', 'Other 4');

是否有更精简的方法来执行此操作,还是需要重复 150 行代码?

Is there a more streamline method of doing this or do I need to repeat the code for 150 lines?

推荐答案

从可用性的角度来看,您可能希望重新考虑表单的设计.如果用户填写了 149 行订单并不小心点击了后退按钮,他们将度过非常糟糕的一天.也许您应该考虑一次捕获一行,或者允许用户上传包含所有条目的文件(电子表格、CSV 等).

From a usablility standpoint, you may wish to reconsider the design of your form. If the user fills out 149 lines of the order form and accidentally hits the back button, they are going to have a really bad day. Perhaps you should consider capturing a single line at a time, or allowing the user to upload a file (spreadsheet, CSV, etc) containing all of the entries.

如果您无法更改表单的设计,则需要循环处理这些行.循环看起来像这样:

If you cannot change the design of the form, you will need to process the lines in a loop. The loop can look something like this:

for ( $i = 1; $i <=150; $i++ ) {
    $name = $_POST['name' . $i];
    $value = $_POST['value1' . $i];
    // capture the rest of the field values
    $query = "INSERT INTO (...) VALUES ($name, $value, ...)";
    mysql_query($query);
}

或者,您可以将每个循环的结果附加到一个大查询中,并在捕获所有 150 行后运行它.

Alternately, you can append the result of each loop to one big query, and run it after capturing all 150 rows.

为了简洁起见,我省略了一些您需要添加的细节:

For the sake of brevity, I have left out a few details which you need to add:

  • 转义您的输入以确保您的用户不会损坏您的数据库
  • 考虑使用 PDO 或 mysql_* 以外的其他东西来简化您的数据库使用.
  • 检查以确保您没有在一行留空时捕获和插入空值.

这篇关于PHP 订单捕获门户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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