Jump to content

submitting all form fields


tkdasearls

Recommended Posts

I am new to this and I was wondering if there is a way to update all form fields that are present.  So here is the situation.  I am storing multiple field types/fields in a database.  The database then gets queried for all the form types and creates a field for each entry.  So lets say that I have 2 entries that are text fields.  I then use PHP to show the fields on the page.  I then want to be able to type into these fields and store them in my database under the column "value".  The only thing that I can't seem to figure out is how I submit these back to the database so that the proper field gets updated with the proper value.  does this make sense?  any help would be greatly appreciated! 

Link to comment
Share on other sites

What Im trying to get at is how would i construct the update query I guess.  I know how to do it if I set the names and the fields aren't created dynamically....but how do I create a query where the fields are created dynamically and there could be anywhere from 1 field to 100 fields with the "id" number being the fields "name".

Link to comment
Share on other sites

You could build your query statement dynamically as you loop thru your input fields. Use vars to represent the field names just like you will be doing with the corresponding values

 

Are you sure you have thought out this design well? Seems kind of crazy.

Link to comment
Share on other sites

I know it sounds kind of crazy.  Like I said, I am newer to this so I am sure there is a better way, Im just not sure what that way would be.  What I'm basically trying to do is build a "form builder" where each of the fields are stored in the database with an id, name, type, and if it is required.  Then for any field type that is a "select" I have a different table where I will store those values.  Do you have any recommendations on a way to easily do this?

Link to comment
Share on other sites

A form builder? You mean to be used in your future endeavors when you need to write a script that will utilize a new form? Or to satisfy a requirement that you build random forms all the time using some massive set of varying inputs? Both seem kind of large tasks for a newbie to be experimenting with.

 

Let's face it - given a task you design a form and write the html for it and use it in your php script. When will you need to re-write THAT particular form again that either a) you can't copy it, or b) well, there is no b. Forms are unique to each application in that you want your own titles for them, your own name= attributes and your own data requirements. If you actually have a future plan that will allow you to re-use the same sets of fields for your future forms then I guess this will work for you, but I think it's a long ways off to be using as a learning experience for html/php/sql knowledge.

 

Just my $.02.

Link to comment
Share on other sites

so, I think I figured out my looping problem so that it creates a query for each field.  What I am running into now is syntax I guess as it isn't updating my database.  here is what I have

$sql = mysql_query("SELECT * FROM form");
while($row = mysql_fetch_array($sql)){
$id =$row["id"];
$name =$row["name"];
$type =$row["type"];
$required =$row["required"];
$line ="Update form Set name='".$_POST[$id]."' Where id='".$id."'";
if ($_POST['submit']){
    mysql_query("".$line."");

}	
echo " ".$line;

}

So what this is doing is it is in code not shown, it is forming my form by grabbing data from the database and creating a form.  this code then is creating the variables for this form for submission to the database.  I think I got the basic structure down to create multiple updates when submit is clicked, but for some (obvious I'm sure) reason, it is not working.  Can anyone offer any assistance to get the update query working correctly?

Link to comment
Share on other sites

could you please post your db schema? tad confused what you're trying to do... 

 

as ginerjm said, if you're updating a row you should just be passing back the id of the row in question using a hidden form field and the user accessible form fields would have names which pertain to their respective database column.

Link to comment
Share on other sites

I have a table named form with fields of id, name, type, and required.  I have a script that currently only inputs a type, but not a name.  the id is auto incremented so so I am using that has the "name" of the fields for now.  my script then pulls these back and creates a form based on what is in this database, with field names of 1,2,3 etc.  Then I need my code that I posted to update the "name" field in the data base when I type into the field in the form that was created. 

 

so if I type in the name of test for id 1, I need that to update in the database accordingly

Link to comment
Share on other sites

oh i see,

I think you'd be best off using a basic relational database and storing the ID rather than having fields with names of 1,2,3 and sending back multiple form fields and expecting to match up the numeric form element name values rather than human readable values...

 

I would create a form table, have that as an incremental ID in that table.

Then join each form element (id,type,name,value etc) to that so add an extra row in there of formId

And when you submit the form to update, send across a hidden form element with formId... that way you know when you're doing the update you can update where formId = x and name = y since a form shouldn't have the same form element name twice.

 

In your case, you can maintain your current code (which I'd advise against) and simply change the form element name to be myinput[$id] so it passes back an array of the form elements to the server and can loop through those to process.... 

i.e.

//lets echo out the form elements 
foreach ($formElements as $row){
    echo "<input type='{$row['type']}' name='myinput[{$row['id']}]' value='{$row['value']}' />"
}

now to update the form elements,

foreach ($_POST['myinput'] AS $k=>$e)
{
$sql = "UPDATE yourtablename SET value="{$e}" WHERE id='$k'";
mysql_query($sql);
}
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.