Jump to content

Post Question


mdmartiny

Recommended Posts

I have some code that pulls the field names from a table to create form form submitting information. I am trying to write this code so that someone who knows nothing about databases, tables and MySQL can make a table of their own by filling in a few text boxes.

 

Now what I was wondering is since I have no idea what people are going to name their table columns so I won't know what information Post information I will be sending to the next step of the process.

 

Is there a way with PHP that will allow me to get unknown post variables? If there is how would I do that?

Link to comment
Share on other sites

Putting aside the above concern wherein you are letting people with no database information near a database...

 

I am not completely understanding your issue. It sounds like you are either misunderstanding how $_POST relays information, or your application is designed poorly.

 

Example:

Let's say your user see's a form to help them design a table...

Table Name: [text box name=name]

Table Stuff: [text box name=stuff]

You can set the name of the text box elements, and then, when the form is submitted, you can look at $_POST['name'] and $_POST['stuff'] to see what the user put into the text box.

 

So, would you mind further elaborating on your original issue? Thanks.

Link to comment
Share on other sites

Although why you would want to let people, who have no idea how to actualy use a database, anywhere near one baffles me.

 

This is something that I am working on for someone that wants to have a classified ad section on there website and want to be able to do all of the work themselves. That way every time that they want to ad something to it they do not have to come to me or some one else.  They do not have direct access to the database just the ability to add

 

TimeBomb

The script that I have written so far allows a person to fill out 2 form fields one for a table name and another for how many columns they want in the table. This then allows them to fill in the column names and and attributes and all of that.

 

What I am doing now is writing a script that goes in and pulls the names of the columns from the table that the user has created and make a form to allow them to fill out for the individual fields.

 

What they do is pick the table that they want to use from a drop down menu which then creates a form for them to fill out

 

Example:

Let's say your user see's a form to help them design a table...

Table Name: [text box name=name]

Table Stuff: [text box name=stuff]

You can set the name of the text box elements, and then, when the form is submitted, you can look at $_POST['name'] and $_POST['stuff'] to see what the user put into the text box.

 

The example that you showed was pretty accurate.

 

What I need to figure out now is since I will not be making the tables myself and the owner will be. How do I get the information that they have entered into the form. When I do not know the names of the columns they are creating. Meaning I will not know the name of the form fields when they are entering the information.

 

I do not know what the names of the columns or the form fields names are going to be.

 

column name one: [text box name=column name one]

column name two [text box name=column name two]

 

Link to comment
Share on other sites

What you are doing is a dynamic form, and it's actually not as SHOCKING OMG as the previous posters have let on.

 

Your names for the form elements should be things like "elements[]" So when they POST the form you get an array of their desired element names. Those are your column names.

Link to comment
Share on other sites

Not for this step yet... I am trying to search online for examples and such and I am finding nothing yet.

 

I just have never done anything like this before with PHP. I have always had control over the form and new exactly what was being put in them.

Link to comment
Share on other sites

 $tablecolumn = "<form name='insert_table' id='insert_table' action='get_database.php' method='post' enctype='multipart/form-data'>
<fieldset>
<legend>table information</legend>";
        $fieldnamesquery = "SELECT * FROM $table";
        $fieldnamesquery_result = mysql_query($fieldnamesquery);

        $i = 3;
        while ($i < mysql_num_fields($fieldnamesquery_result)) {

		$header = str_replace("_"," ",(mysql_field_name($fieldnamesquery_result, $i)));
		$name = mysql_field_name($fieldnamesquery_result, $i);

            (mysql_field_name($fieldnamesquery_result, $i) == 'image' ? $tablecolumn .= "<p>" . $header . ": <input name='" . $name . "' id='" . $name . "' type='file' /></p>" : $tablecolumn .= "<p>" . $header . ": <input name='" . $name . "' id='" . $name . "' type='text' /></p>");

            $i++;
        }
        $tablecolumn .= "
<p>
<input type='submit' name='submit_info' id='submit_info' />
</p>
</fieldset>
</form>
";

        echo $tablecolumn;

Link to comment
Share on other sites

Alrighty, your problem is still a bit confusing, but I'll see if I can help you.

 

You should be able to run a SELECT * query that will return column datas as well as rows. You know how you usually look at specific row data by using something along the lines of $row['name']? Well, use array_keys($row) to see every single key (i.e. column name) in the array.

 

You can also parse the string that the user has entered into the form, if you have access to that data. Look into the explode function to parse the string of column names.

Link to comment
Share on other sites

Will that also give me the values that are entered into the text boxes?

 

I still don't understand what you're asking then. Once you've constructed your form, try filling it out and submitting, then print_r($_POST) and you can see exactly what gets posted.

Link to comment
Share on other sites

I did some playing around and I got this to work

 

$table = $_POST['table'];

	$fieldnamesquery = "SELECT * FROM $table";
        $fieldnamesquery_result = mysql_query($fieldnamesquery);

        $i = 3;
        while ($i < mysql_num_fields($fieldnamesquery_result)) {

		$post_info = htmlspecialchars(mysql_real_escape_string($_POST[mysql_field_name($fieldnamesquery_result, $i)]));

            echo "<p>".$post_info."</p>";

            $i++;
        }

 

Now all I need to do is figure out how to get the post information into individual variables

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.