Jump to content

php forums dynamic fields


bigmuddyfoot

Recommended Posts

Hello everyone!I have a few questions, I have taken on a project this week that is kinda complex. My first question regarding this script is this: I am going to have a forum with a few drop downs text fields and text boxes and check boxes/selections. for this example lets say there is a total of 12 elements (from the above list) Thats the easy part no what i need to do is. I am going to have a $var that can be anything from 1 to 150, now i need the forum drop downs text fields, text boxes and check boxes/selections to repeat with in that forum for the amount of $var. Then when all that info is field out i need it to post to a mysql db. What i dont understand how to do is make php rename all the name boxes for each example by default i will have: |--fname--|--lname--|--start_date--| --end_date--|--night--| and so on.... So what im assuming i need to do is make php do (with $var being 3)... |--fname--|--lname--|--start_date--| --end_date--|--night--| and so on....|--fname1--|--lname1--|--start_date1--| --end_date1--|--night1--| and so on....|--fname2--|--lname2--|--start_date2--| --end_date2--|--night2--| and so on.... And how do i get my php insert mysql script to dynamically know names of the fields (fname, fname1, fname2....and so on) and have them stored in mysql. Thank you for the help!Kelly

Link to comment
Share on other sites

If you use array notation on the <input> names then you can access them as an array in PHP.

Field 1: <input type="text" name="text[]">Field 2: <input type="text" name="text[]">Field 3: <input type="text" name="text[]">

In PHP:

$query = 'INSERT INTO table (field) VALUES ';foreach($_POST['text'] as $t) {    $query .= "($t),";}// Remove the last comma$query = substr($query, 0, -1);

Since you have multiple fields, it may be better to go through them with a for() loop.

Link to comment
Share on other sites

Foxy Mod Thank you for your help just to make sure i understand right the code below where the text[] is that will change automatically to text0 text1 text2 and so on?

Field 1: <input type="text" name="text[]">Field 2: <input type="text" name="text[]">Field 3: <input type="text" name="text[]">

And

$query = 'INSERT INTO table (field) VALUES ';foreach($_POST['text'] as $t) {    $query .= "($t),";}// Remove the last comma$query = substr($query, 0, -1);

This will just save every thing as a new row in mysql

Link to comment
Share on other sites

Ok, I think i have this right?

<?$var=$_GET['total'];$i=1;while($i<=$var)  {?> Field 1: <input type="text" name="text[]">Field 2: <input type="text" name="text[]">Field 3: <input type="text" name="text[]">  <?  $i++;  }  ?>

Then to retrive them i use

 <? foreach($_POST['text'] as $t) {	$query .= "($t),";}echo $query;?>

What i dont under stand is this.here is mysql *******************************************************************|id| |fname| |lname| |startd| |datee| |night||1| |Kelly | |Hansen| |102412| |103012| |M||2| |matt | |blablabl| |100912| |101412| |T||3| |jessi | |blablabl| |101912| |102112| |Th| *******************************************************************This is where i would like to use the following code because there will be a different amount of fields used every time.

<?$var=$_GET['total'];$i=1;while($i<=$var)  {?> Field 1: <input type="text" name="text[]">Field 2: <input type="text" name="text[]">Field 3: <input type="text" name="text[]">  <?  $i++;  }  ?>

Then use

$query = 'INSERT INTO table (field) VALUES ';foreach($_POST['text'] as $t) {	$query .= "($t),";}// Remove the last comma$query = substr($query, 0, -1);

To post it in the db how am i going to do that if my results from the array come out as: (this is data 1),(this is data 2),(this is data 3),(this is data 4),(this is data 5),(this is data 6),(this is data 7),(this is data 8),(this is data 9),(this is data 10),(this is data 11),(this is data 12) With this being my code from the forum page...

<form name="input" action="submit.php" method="POST"><?$var=$_GET['total'];$i=1;while($i<=$var)  {?>Field 1: <input type="text" name="text[]">Field 2: <input type="text" name="text[]">Field 3: <input type="text" name="text[]">  <?  $i++;  }  ?>  <input type="submit" value="Submit">  </form>

Edited by bigmuddyfoot
Link to comment
Share on other sites

The SQL INSERT query allows you to add as many rows as you like. My loop is an example of how to do it automatically. The intention is to create a query like this:

INSERT INTO table (field1, field2, field3) VALUES('value00', 'value01', 'value02'),('value10', 'value11', 'value12'),('value20', 'value21', 'value22')

Supposing that you have multiple fields that use the array syntax here's a way to add them all with a single query:

$fnames= $_POST['fname'];$lnames= $_POST['lname']; $length = count($fnames); $query = 'INSERT INTO table (fname, lname) VALUES ';for($i = 0; $i < $length, $i++) {	// For safety, mysql_real_escape_string() is applied	$query .= "('{$fnames[$i]}', '{$lnames[$i]}'),";} // Send the $query to the database library you're using

Link to comment
Share on other sites

I cant seam to get it to work what am i doing wrong?The script runs and does not error out, but nothing is saved to the db Here is my submit.php page (this saves it to the db)

 <?php$con = mysql_connect("*********","**********","**********!");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("donavons_class_schedule", $con);$query = 'INSERT INTO felds (one, two, three) VALUES ';foreach($_POST['text'] as $t) {    $query .= "($t),";}// Remove the last comma$query = substr($query, 0, -1);mysql_close($con);?>

Here is the index.php page (the page with the forum on it)

<form name="input" action="submit.php" method="POST"><?$var=$_GET['total'];$i=1;while($i<=$var)  {?>Field 1: <input type="text" name="text[]">Field 2: <input type="text" name="text[]">Field 3: <input type="text" name="text[]">  <?  $i++;  }  ?>  <input type="submit" value="Submit">  </form>

And here is the picture of the dbdb.JPG(http://www.php.donavonscreativeinnovations.com/db.JPG)

Link to comment
Share on other sites

One problem is that you're not actually running the query, you just build it but never run it. Even so, you're only listing 1 value per row when you're telling it to expect 3. You're going to end up with a query like this: INSERT INTO feld (one, two, three) VALUES('value'), ('value'), ('value') ... You're telling it to expect 3 values, but then you only give it 1.

Link to comment
Share on other sites

Ok still not working here is what i have

<form name="input" action="submit.php" method="POST"><?$var=$_GET['total'];$i=1;while($i<=$var)  {?>Field 1: <input type="text" name="fname[<? echo $i; ?>]">Field 2: <input type="text" name="lname[<? echo $i; ?>]">Field 3: <input type="text" name="phone[<? echo $i; ?>]">  <?  $i++;  }  ?>  <input type="hidden" name="count" value="<? echo $_GET['total']; ?>">  <input type="submit" value="Submit">  </form>

<?$con = mysql_connect("localhost","*************","*************");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("donavons_class_schedule", $con); $i=1;$var=$_POST['count']*3;$query = 'INSERT INTO felds (one, two, three) VALUES ';while($i<=$var)  {echo $_POST['fname'][$i];echo $_POST['lname'][$i];echo $_POST['phone'][$i];$i++;  }mysql_close($con);?>

Still does not add to db

Edited by bigmuddyfoot
Link to comment
Share on other sites

You're still not doing anything with the query. You use the mysql_query function to send a query to MySQL. You're not doing that, you're not doing anything with the database at all. You connect to the database server, select a database, run some other code, and then close the database connection. You never ask the database to do anything. That being said, once you get your code set up to send $query to the database, before you do that print out $query so that you can see what it looks like. It needs to follow this format: INSERT INTO table (field1, field2, field3) VALUES('value00', 'value01', 'value02'),('value10', 'value11', 'value12'),('value20', 'value21', 'value22')

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...