Member Avatar for skinbug

Hi, I've looked at various tutorials/forums at this but I still can't get my head around it...

The senario is a html form processed by php, where the user has the option to check some boxes. Once the user has submitted the form, the admin wants to be able to see the results of the boxes checked at registration.

The main part I don't understand is when how the values are stored in MySQL. For a text input for example, I have written the sql like this... firstname varchar(30) not null All I have seen in tutorials is that the checkbox names need to be an array, but it doesn't say how they are stored in the sql, eg their data type.

Do I also need to create a row for the checkbox values?
Do I need to create multiple rows since there are about 30 checkboxes?

Please can anyone help with this, to make me understand it more clearly?

Your HTML checkboxes need to have the same name, and a special syntax just for HTML arrays, as in checkboxes:

<input type="checkbox" name="cb[]" value="a" />
<input type="checkbox" name="cb[]" value="b" />
<input type="checkbox" name="cb[]" value="c" />

PHP will automatically receive this as an array. Assuming all checkboxes are checked, this would be the script you are posting to...

$cb = $_POST['cb']; //nothing else to do, this value already an array
print_r($cb);

yields

Array (
  1 => a,
  2 => b,
  3 => c
)

to store in a database, you create a comma-separated value

$csv_cb = implode(',',$cb);

...which yields a value of 'a,b,c' that is easily stored in and retrieved from a database.

Member Avatar for skinbug

hi thanks for responding...had to sleep last night!

So to sotre in the database, would the values of a,b,c be put into their own row which would be created dynamically, or would their vaues already sit there?

This is the bit I don't get...do I need to edit the sql file and create rows for a,b,c, that a user can then interact with through the checkboxes?

Member Avatar for skinbug

To be a bit more clear, I need to insert values into the database to start with, so do the rows need to pre-exist or are they somehow created dynamically?

Much help needed explaining this bit!!!

No, "a,b,c" is one value that is stored in one field. Think of the group of checkboxes as being one field in the database. When you want to review what someone selected, you pull the store value from the database and parse the comma-separated string.

In this case, we know that the use checked all three boxes.

As for your datatype in the sql db, varchar is perfect. Just make sure the length is enough to hold all the values of the checkboxes plus the commas, and then add 20 or so. You never know if/when you'll want to add more checkboxes in the future.

As for "so do the rows need to pre-exist or are they somehow created dynamically?" I have to admit, I am at a loss. A row in a db can be thought of as a single record. Each new submission by your user will create a new row/record in the database.

You can think of a database like a spreadsheet if it helps. Again, each row is a record, and each column is a field. You might have three columns: user, timestamp, selections.

user - who completed the submission
timestamp - when
selections - the csv value we've discussed = the boxes that were checked

Member Avatar for skinbug

Since starting this I have realised that the sql would need to have a field defined, similar to what you suggested... selections varchar(20) null My wording has not been entirely accurate either...when I said rows, I meant columns (or fields as you say).

So far, I have the chkboxes sorted, the sql sorted and now I need to POST the things, and then retrieve the data somehow.

Problem I have is that the whole script is object orientated, and posting a user input goes through about 4 steps (functions) on about 3/4 different php pages.

This is the reason I have not really posted much code on this since, it is all over the place...headache continues!!!

Thanks for your help so far, you've cleared up a few things for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.