Multiple form values posted to one mySQL table column?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
davids0208
Forum Newbie
Posts: 2
Joined: Thu Sep 11, 2008 2:13 pm

Multiple form values posted to one mySQL table column?

Post 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)
?>
 
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

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

Post 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).
davids0208
Forum Newbie
Posts: 2
Joined: Thu Sep 11, 2008 2:13 pm

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

Post by davids0208 »

Because the real form that I am creating has around 100 options.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post by JAB Creations »

Mods: please move this. OP: read the forum notes before posting! :nono:
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

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

Post 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!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

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

Post 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
Post Reply