1

I have a form that is made up of lots of radio buttons and check-boxes, and I was wondering what the best way to save their values to the database is. There are a few things to take into account.

  1. All the the radio/checkboxes have different names. I've already created a new table with a column for each radio/checkbox, 30 new columns in total. I was advised this way is better than storing multiple checkboxes values in a single column.

  2. I am working on a system based on the MVC Model, and this is the code I have in the controller file to send the radio/checkbox value to the database

    if (isset($this->request->post['buss_type'])) {
        $this->data['buss_type'] = $this->request->post['buss_type'];
    } else {
        $this->data['buss_type'] = '';
    }
    
  3. I was wondering if I am forced to duplicate the previous code for my 30 radio/checkboxes, or is there's a simpler way in which I could put all radio/checkboxes together?

  4. I'd appreciate it very much if you are really specific since I just started learning PHP.

EDIT DEMO ADDED Click on "Corporate Account" to see the form.

1
  • 1
    You should almost NEVER store multiple values in one column. The one exception would be combined binary values as a single integer, and even that only works for specific systems. Commented Mar 15, 2012 at 22:03

4 Answers 4

1

Well this will help with Q.3 at least

foreach(array('buss_type','anotherfield','anotherfield','...etc') as $index)
{
  if (isset($this->request->post[$index])) { $this->data[$index] = $this->request->post[$index]; } else { $this->data[$index] = NULL; }
}
Sign up to request clarification or add additional context in comments.

7 Comments

with this answer, you would then have to edit both the array, and the html, anytime one of the fields change. not to mention you would have a long ugly array in your code. see my answer for a different take. create the array client side.
One thing I would be concerned with his how the DB query is built up from the data, ie do you have to account for someone using name="check[id]" in the request and interfering with columns that are not part of the check box set? my approach would create a white list of fields to use. and yes you're right, a double up of the list of check boxes names to manage would be created
Thanks a lot for your answers. This is the code I was looking for. @dqhendricks I like your recommendation, too, but I was recommended that previously but I never understood how I to exactly code it. Again, all the values have to go to unique columns in the database, so if for example, I have check[something], how do I have to name the table column for that field?. I Will add a demo of the form in a minute so you guys can take a look at it. Thanks.
@codekmarv if the input was named 'check[somekey]', when you do the loop I wrote below, $key will contain 'somekey', and $value would contain the value of that input.
so in that case what's the name of the column in the database to store the value of somekey? Is it check[somekey] or just "somekey"? Thanks a lot for you help.
|
1

Sounds like you need a query builder. Heres an example of one of mine

$guide=array(
"active"=>"Active Properties",
"incomplete"=>"Incomplete Properties",
"default"=>"Default (Borders and Headers)   ",
"links"=>"Link Color",
"background"=>"Background Color",
"navcolor"=>"Navigation Color");

$s="UPDATE dashboard_layout SET ";
$c="";
$vs="";
foreach($guide as $k=>$v){
$s.="`$k` = \"".mysql_real_escape_string($_POST[$k])."\"".($k=="navcolor"?"":",")." ";
$c.="`$k`".($k=="navcolor"?"":",");
$vs.='"'.mysql_real_escape_string($_POST[$k]).'"'.($k=="navcolor"?"":",");
}
$n="INSERT INTO dashboard_layout ($c,lender_id) VALUES ($vs,$company)";
$s.="WHERE lender_id='$company'";
$sql=($editmode?$s:$n);
$db->run($sql);

Keep in mind you can still use foreach with objects. I recommend using a guide

1 Comment

Thanks, but I actually don't understand how this can help me. I guess I am too noob :(
1

if you treat all of the checkboxes the same, you could simply make them an array within your html form.

<input name="check[footype]" type="checkbox" value="1" />
<input name="check[bartype]" type="checkbox" value="baz" />

Then just loop through them server side.

foreach ($_POST['check'] as $key => $value) {
   // repeated logic here

}

Basically by naming the inputs in your form this way, you are making $_POST['check'] contain an array with all of your check values server side which you can loop through.

1 Comment

He's right, you would have to keep in mind that the data from the user could not be trusted. Users could slip any data in the array with any array key.
1

adding to dqhendricks reply, if you are building the query yourself and not through a framework, this might help you (I haven't tried it though!):

$values = array();
foreach ($_POST['check'] as $key => $value ) {
    // I advise you to use either PDO or mysqli but to simplify:
    $values = "'" . mysql_real_escape_string($value) . "'";
}

// all values will be separated by a comma
$values_list = implode(',', $values);

$query = "insert into table_name values ($values_list)";

1 Comment

But again ... isn't it that it's not recommended to store all values in a single column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.