mdmartiny Posted April 3, 2012 Share Posted April 3, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/ Share on other sites More sharing options...
Muddy_Funster Posted April 3, 2012 Share Posted April 3, 2012 You could pull the entire $_POST into an array and work through indexed values rather than names. Although why you would want to let people, who have no idea how to actualy use a database, anywhere near one baffles me. Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1333860 Share on other sites More sharing options...
TimeBomb Posted April 3, 2012 Share Posted April 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1333863 Share on other sites More sharing options...
mdmartiny Posted April 3, 2012 Author Share Posted April 3, 2012 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] Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1333979 Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334042 Share on other sites More sharing options...
mdmartiny Posted April 3, 2012 Author Share Posted April 3, 2012 How would I do that this is something new to me? Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334067 Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 How do you do what I said? Try it and see. Do you have any code written? Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334068 Share on other sites More sharing options...
mdmartiny Posted April 3, 2012 Author Share Posted April 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334081 Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 Sorry, where are you stuck? I reread your post and I don't understand at what step in the process you are stuck. Break it down into simple 1 sentence steps for me. Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334097 Share on other sites More sharing options...
mdmartiny Posted April 3, 2012 Author Share Posted April 3, 2012 I need to get the information that is entered into the dynamic form field out of it. I have the form created just need to get the information inputted out. Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334099 Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 Right now, what does the code look like that generates the dynamic form based on their desired column names? Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334100 Share on other sites More sharing options...
mdmartiny Posted April 3, 2012 Author Share Posted April 3, 2012 $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; Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334106 Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 So you know what the names should be, when you go to process that form, again select the desired column names just like you did here, and loop through them to process them. Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334117 Share on other sites More sharing options...
mdmartiny Posted April 3, 2012 Author Share Posted April 3, 2012 Will that also give me the values that are entered into the text boxes? Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334145 Share on other sites More sharing options...
TimeBomb Posted April 4, 2012 Share Posted April 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334208 Share on other sites More sharing options...
Jessica Posted April 4, 2012 Share Posted April 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334293 Share on other sites More sharing options...
mdmartiny Posted April 5, 2012 Author Share Posted April 5, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260230-post-question/#findComment-1334756 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.