Page 1 of 1

Multiple form values posted to one mySQL table column?

Posted: Thu Sep 11, 2008 2:24 pm
by davids0208
I would like to take the values of a form and post all of them to one column of my mySQL database table.

Example:
Lets say I have 3 textfields called text1, text2, and text3 and the user enters into the text1 field the letter x, the text 2 field the letter y and the text3 field the letter z. When the user clicks the submit button the values of those 3 textfields would be passed to the database table column called textFieldValues and look like this:

$text1 = "x", $text2 = "y", $text3 = "z"

Then I could later pull the data down from that same table and use all those variables in a different page.

Many thanks to anyone who could help. The code i am starting from looks like this

Code: Select all

 
<?php
$con = mysql_connect("localhost","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("my_db", $con);
 
$sql="INSERT INTO my_table (textFieldValues)
VALUES
('$_POST[text1]','$_POST[text2]','$_POST[text3]')";
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
 
mysql_close($con)
?>
 

Re: Multiple form values posted to one mySQL table column?

Posted: Thu Sep 11, 2008 4:38 pm
by jayshields
Is this a specific reason you want to store 3 pieces of data in one field and have references to the original variable names? If not, you're going about it the wrong way. It's easier and more efficient to put them in seperate fields and scrap variable names (they're meaningless).

Re: Multiple form values posted to one mySQL table column?

Posted: Thu Sep 11, 2008 6:03 pm
by davids0208
Because the real form that I am creating has around 100 options.

Re: Multiple form values posted to one mySQL table column?

Posted: Thu Sep 11, 2008 6:31 pm
by JAB Creations
Mods: please move this. OP: read the forum notes before posting! :nono:

Re: Multiple form values posted to one mySQL table column?

Posted: Thu Sep 11, 2008 7:06 pm
by jayshields
davids0208 wrote:Because the real form that I am creating has around 100 options.
Options, as-in, seperate pieces of data? Sounds like an extraordinary, or badly designed form. If you've really got 100 seperate pieces of data to be collected, then you might need a 100 field table!

Re: Multiple form values posted to one mySQL table column?

Posted: Sat Sep 13, 2008 3:44 am
by josh
Forms with 100+ options tend to change frequently. You could make a table that just keeps track of the different fields, and just have a 3 field table for the response that store the field_id, the value and the response_id. You could select all the answers for a given response_id and get all the values for all the fields. Unless you need to do a lot of joining on the data and such thats how I'd do it. Then it would be a lot easier to break the form up into a multiple page form later, if needs change / grow