使用mysql和php创建一个多步骤的过程

I am working on a script which allows users to create a stepped process.

The steps will consist of sending visitors to urls where they will fill out a form then be directed to the next step the user has created. Each user can create as many steps as they wish and the url for each step will be unique.

I am not sure if I am doing this in the correct/most efficient way so I have a few questions about this. Before I begin here is a simple version of my table structure:

step_id     user_id       step_url                   step_order
  1           1       example.com?step=1              1
  2           2      test.com?step=1                  1
  3           1       example.com?step=3              2

A visitor will be directed to step_url: example.com?step=1 by user_id: 1

On this page there will be a form which contains a hidden field with the value of the step_id like this:

<input type="hidden" name="step_id" value="1">

Once the visitor has filled out this form it will be processed by my script at a url similar to: http://mysite.com/form-process.php

Once the form has been submitted I then need to direct the visitor to the next step my user has created.

I would currently do this using code similar to this:

SELECT step_url FROM table WHERE step_order='$current_step_order'+1

Here are my questions about this:

  1. Is this the best most efficient way to accomplish this? Or is there a better way?

  2. As the step_id column is auto increment...how would I increment the order column by 1 each time a user inserts a new step?

Thanks for taking the time to read this.

Paul

I think steps in your problem have parent child relationship. so it would be better if you refer steps with whose child is that step. you can use step no as auxiliary field.

this would be the schema

step_id     user_id       step_url             parent_id      step_order
  1           1       example.com?step=1        null           1
  2           2      test.com?step=1            null           1
  3           1       example.com?step=3        1              2

and for finding next step you can call

SELECT step_url FROM table WHERE parent_id='$current_step_id'

you can see more about hierarchical data http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

Nitin Sharma

Your post is well-written but at the same time it is hard to make sense of it. Here's my attempt to answer it.

Here's one way to do it: Method 1

When a user connects to your web page you can assign them a user_id based on session info, IP, etc. Whenever a user_id completes a step in the process, you can update your database to reflect the step that user_id is on.

When a user makes a GET request of your web page, you can look up the step they are on and provide them with the appropriate form. The creation of the form will be a server-side script (e.g. PHP). When they submit the form, you can simply process the POST data, increment the step they are on, (updating both the variables in the script and the tables in your database), and provide them with the next form.

Another way: METHOD 2

Another way to do it is to send them all the forms they have to fill out at once, on their very first GET request. You can hide/display certain forms using JavaScript or another client-side scripting language, according to the step they are on. The benefits of this is that the process of completing all the steps (i.e. filling out all the forms) will be much faster (there won't be a server-side query after each step). The downside is that there will be a longer download time and a longer upload time (at the end of all the steps when your massive container form is processed).

@@@@@@@@@@@@

In both of the plans I have provided, I have assumed that you are doing this all on your own web domain. If you are going to have them visit OTHER web sites, and fill out forms on OTHER web sites, then the issue is more difficult. It is possible that you can do it using internal frames, but you would have to receive notification of when a form in an internal frame is submitted. Another possibility is by emulating the forms on foreign domains, however, if it is a sensitive login or whatnot it will be hard to emulate.