Jump to content

Form with checkboxes


harkly

Recommended Posts

I have a form with checkboxes and I want to insert the selected info into my database all I am getting is either "Array" or when I serialize it I get -- a:3:{i:0;s:1:"2";i:1;s:1:"9";i:2;s:2:"10";} -- depends on what is selected.  So I know that I have a connection but I am not sure if this is what it is suppose to look like or not - can someone tell me if it is or what I need to do to get the data in my DB?

 

<?php
   include('library/login.php');
   login();

  mysql_select_db('bariatric'); 
  $search=$_GET['userID'];

$result = mysql_query("SELECT * FROM interests WHERE userID = 'kelly' LIMIT 1");

if (mysql_num_rows($result) == 1) 

   {

   $userID=$r['userID'];
   $sports=$r['sports'];
   $exercise=$r['exercise'];
   $exercise_type=$r['exercise_type'];
   $outdoors=$r['outdoors'];
   $artistic=$r['artistic'];
   $tv_shows=$r['tv_shows'];
   $music=$r['music'];
   $free_time=$r['free_time'];
   $favorite_places=$r['favorite_places'];
   $interests=$r['interests'];
   $favorite_things=$r['favorite_things'];
}

   echo "


<form method='post' action='updateInterests.php'> 
Choose a interest:<br><br> 
<input type='checkbox' name='interests[]' value='2'>Aerobics<br> 
<input type='checkbox' name='interests[]' value='3'>Cycling<br> 
<input type='checkbox' name='interests[]' value='4'>Jogging / Running<br> 
<input type='checkbox' name='interests[]' value='5'>Mountain biking<br> 
<input type='checkbox' name='interests[]' value='6'>Racquetball<br> 
<input type='checkbox' name='interests[]' value='7'>Rock climbing<br> 
<input type='checkbox' name='interests[]' value='8'>Rollerblading<br> 
<input type='checkbox' name='interests[]' value='9'>Swimming<br> 
<input type='checkbox' name='interests[]' value='10'>Walking / Hiking<br> 
<input type='checkbox' name='interests[]' value='11'>Weights / Machines<br> 
<input type='checkbox' name='interests[]' value='12'>Yoga<br> 

<input type='submit' name = 'submit'> 
</form> 

\n";
?>

 

My update code::

 

<?php
  mysql_select_db('bariatric'); 

   $userID=$r['userID'];
   $sports=$_POST['sports'];
   $exercise=$_POST['exercise'];
   $exercise_type=$_POST['exercise_type'];
   $outdoors=$_POST['outdoors'];
   $artistic=$_POST['artistic'];
   $tv_shows=$_POST['tv_shows'];
   $music=$_POST['music'];
   $free_time=$_POST['free_time'];
   $favorite_places=$_POST['favorite_places'];
   $interests=serialize($_POST['interests']);
   $favorite_things=$_POST['favorite_things'];


$query = "UPDATE interests SET sports='$sports', exercise='$exercise', exercise_type='$exercise_type', outdoors='$outdoors', 
           artistic='$artistic', tv_shows='$tv_shows', music='$music', free_time='$free_time', favorite_places='$favorite_places',
           interests='$interests',favorite_things='$favorite_things'
          WHERE userID='kelly'";


$result = mysql_query($query) or die(mysql_error());


      if ($result)
      {
         echo "<div id='aboutForm'>Information changed.</div> \n";

      }
      else
      {
         echo "<h2>Sorry, I could not change the information.</h2>\n";
      }



?>

Link to comment
https://forums.phpfreaks.com/topic/183918-form-with-checkboxes/
Share on other sites

how are you planning on storing $interests in the db?  it being an array, you need to turn that array into a string for insertion into the db.

 

you should consider changing your layout for "interests" .. what i mean by that is that you should create a separate table called interests, and create a field for each, aka., database normalization.

 

so, you would have each interest as a field, ie: Aerobics | Cycling | Jogging_Running and so on, and then a 1 or 0 for whether that user has one as an interest.

 

'cause the only alternative would be to convert the array into a string using a delimiter method like so:

 

<?php
$interests = implode ('|', $_POST['interests']); //not a good way of storing information;
?>

 

but it is a very impractical way as retrieval becomes more and more difficult down the road since you can't run query on people who are interested in certain things.  just doesn't work.

 

i'm not so much just suggesting you go the route aforementioned as i am strongly encouraging it.  and i guarantee you it will save you headaches in the future .. and it's a piece of cake to setup.  if yo need help, just ask.  and when you've got the table setup, we'll talk queries, as well as how to populate checkboxes with database drawn information.

Link to comment
https://forums.phpfreaks.com/topic/183918-form-with-checkboxes/#findComment-970985
Share on other sites

Thanks for the input!!!! Much appreciated!!

 

Right now I have a table in my DB called interests with a field called interests ( I should probably have a different field name?)

 

CREATE TABLE interests (interests_id VARCHAR(32) NOT NULL PRIMARY KEY,

userID VARCHAR(32) NOT NULL,
sports BOOL,
exercise VARCHAR(35),
exercise_type VARCHAR(35),
outdoors BOOL,
artistic BOOL,
tv_shows  VARCHAR(75),
music VARCHAR(35),
free_time VARCHAR(150),
favorite_places VARCHAR(150),
interests VARCHAR(50),
favoritie_things VARCHAR(150));

 

And if I understand you, it would be better to break that down the table to smaller tables

 

for ex

CREATE TABLE interest (interestID TINYINT(2) NOT NULL PRIMARY KEY,

userID VARCHAR(32) NOT NULL,
aerobics TINYINT(2) 
cycling TINYINT(2)
jogging TINYINT(2)
etc....

CREATE TABLE music (musicID TINYINT(2) NOT NULL PRIMARY KEY,

userID VARCHAR(32) NOT NULL,
Rock TINYINT(2)
Pop TINYINT(2)
etc....

 

 

Then I can join them together with a query.

 

Can you update more than one table in the database at a time with one form? As well as call info from various tables into one form?

 

 

 

 

 

I have been working on pulling info from the db to populate the form and have been able to do so with radio and drop down menus but not with the checkbox where I want to be able to select more that one option.

 

This is what I have for another page where I have type='radio'

 

if ($wls == 1){
                   echo "

               <input type='radio' name='wls' value='1' checked>DS - Duodenal Switch
   	       <br><input type='radio' name='wls' value='2'>LAP - Lap-Band 
       <br><input type='radio' name='wls' value='3'>MGB - Mini-Gastric Bypass
       <br><input type='radio' name='wls' value='4'>RNY - Roux-en-Y Gastric Bypass
       <br><input type='radio' name='wls' value='5'>VBG - Vertical Banded Gastroplasty
       <br><input type='radio' name='wls' value='6'>VSG - Vertical Sleeve Gastrectomy
       <br><input type='radio' name='wls' value='7'>Other <input type='text'name='wls_other' size='15' class='zip'> 
       <br><input type='radio' name='wls' value='8'>None \n";}

                 else if ($wls == 2){
                   echo "
               <input type='radio' name='wls' value='1'>DS - Duodenal Switch
   	       <br><input type='radio' name='wls' value='2' checked>LAP - Lap-Band 
       <br><input type='radio' name='wls' value='3'>MGB - Mini-Gastric Bypass
       <br><input type='radio' name='wls' value='4'>RNY - Roux-en-Y Gastric Bypass
       <br><input type='radio' name='wls' value='5'>VBG - Vertical Banded Gastroplasty
       <br><input type='radio' name='wls' value='6'>VSG - Vertical Sleeve Gastrectomy
       <br><input type='radio' name='wls' value='7'>Other <input type='text'name='wls_other' size='15' class='zip'> 
       <br><input type='radio' name='wls' value='8'>None \n";}

 

If I can break down my tables I am pretty sure I can simplfy the code.

 

Again thanks for the input!

Link to comment
https://forums.phpfreaks.com/topic/183918-form-with-checkboxes/#findComment-971271
Share on other sites

"music" is a perfect example.  having it in a separate table with the types of music is the step you want to take.  this way, if down the road you want to implement a search mechanism to your site, people can now search for users who like music, but who also like Rock, Pop, etc.

 

your TINYINT value can be 1 instead of two .. typically, i have a 1 or 0 method of determining if a specific field applies, so there is only ever going to be a value consisting of 1 digit within that field.  1 means it applies, 0 it doesn't.  just how i do it.  i bet there are some dudes on here who do it a different way, but this works for me.

 

You're basically just breaking your categories/subcategories down to their lowest form.  this just makes the data much more usable. 

Link to comment
https://forums.phpfreaks.com/topic/183918-form-with-checkboxes/#findComment-971343
Share on other sites

check this out: Introduction to Database Normalization

 

yes, you can do multiple database queries based on inputted data from one form (you can do INSERT's, SELECT's, UPDATE's, etc.), so don't worry about that.

 

and no, with all the options available within the MySQL syntax (JOIN's, etc.), this process is definitely the best way to go.  as long as the relationships are set up properly, and the process is as efficient as possible, 5+ queries will go by in a blink of an eye, so don't worry about that either.

 

and there are some dude's on here who know their SQL, just make sure and post in the MySQL Help forum with your MySQL related needs, and you'll get the help you need, and then some.

Link to comment
https://forums.phpfreaks.com/topic/183918-form-with-checkboxes/#findComment-971421
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.