PDA

View Full Version : databases, and forms


Trusten
09-04-2002, 07:51 PM
for some reason, whenever i have a form where a person can choose multiple things, always, the last thing is what is entered into the database, and the rest are ignored.

like if i have a few check boxes for

sugar
water
milk
oil
eggs

at the end, if the person chose them all, only eggs would reach the database.

what can i do?

Spookster
09-04-2002, 07:58 PM
Sounds like you probably gave all your checkboxes the same name? As PHP parses the variables from the form then it would go with the last one it encounters.

Trusten
09-04-2002, 09:57 PM
yes, but i need them all in one colmn.

Spookster
09-05-2002, 12:03 AM
You can arrange them however you like but you will still need to give each one a distinct name in order to have each one available to your php script.

Trusten
09-05-2002, 12:04 AM
yes, but if i do that, how will i get them all into the one area?

Spookster
09-05-2002, 01:46 AM
The naming of a checkbox has absolutely no bearing on it's position.

<form method="post" action="script.php">
<input type="checkbox" name="foo"><br>
<input type="checkbox" name="bar"><br>
<input type="checkbox" name="yaks"><br>
</form>

Trusten
09-05-2002, 01:50 AM
what i mean is this,

i want those all to be placed in the same area in the table, then retrieved from the same spot.

like this

Atributes:
foo, bars, yaks


then on the page, it posts atributes

does this make sense?

Spookster
09-05-2002, 01:51 AM
Do you mean HTML table or database table?

Trusten
09-05-2002, 01:55 AM
database

Spookster
09-05-2002, 01:57 AM
Is each checkbox going to be a seperate column in your database table? Why don't you post some code; that would help to understand what you are trying to do.

Trusten
09-05-2002, 02:04 AM
well it's like this

it's a form to ad a story

in the form it says


Warnings: (please select all that apply)
[] Violence [] Adult content [] Adult language
[] Other[======]

then there's a box for the other.

now, when i post it, it's all going to be

SELECT warnings FROM stories WHERE title = 'This old house'

and then on the page it'll say,

Warnings Include: $warnings

and it should list all that the person choose. if they chose voilence, adult language, etc. etc. etc.

does that help?

Spookster
09-05-2002, 02:18 AM
Ok I understand what you want now. So for each story there could be one or more warnings. How are your tables currently set up? Is it a properly designed relational database? Meaning do you have your stories stored in one table and the warnings stored in another table or are you storing all in one table in which case you would have many null fields when a particular story doesn't have values for all the warnings field?

Trusten
09-05-2002, 02:24 AM
it's all in one table. honestly, i don't know what 'null' means, or 'not null', ya know? i guess that means it can't be blank? right?

Spookster
09-05-2002, 02:42 AM
So now that we know what you are trying to do then what is your question specifically? In your first post it sounded like you were having trouble inserting the data into the database and in your last posts it sounds like you are having trouble pulling the data from the database.

Is your problem strictly PHP related or are you having trouble with your SQL queries?

Trusten
09-05-2002, 02:46 AM
don't know if it's only php.

the trouble is that in the

[] Violence [] Adult language [] Other [=====]

area, whenever someone checks all 3, that only the last one actually gets inserted into the table.

i want that if all 3 are selected, all 3 go into the same 'warnings' column.

stuntboy
09-05-2002, 03:26 AM
How about:

<form method="post" action="script.php">
<input type="checkbox" name="foo[]" value="Violence"><br>
<input type="checkbox" name="foo[]" value="content"><br>
<input type="checkbox" name="foo[]" value="language"><br>
</form>

then you can have:
(you will have to use $_POST if the register globals is off)
$warnings=implode(",",$foo);

warnings will be something like (if all is checked)

Violence,content,language

you can joing them however you want I just used a comma and the values can be what you want too.

There is a drawback to this it makes it very hard to do client side validation of those elements.

Spookster
09-05-2002, 03:50 AM
Originally posted by Trusten
don't know if it's only php.

the trouble is that in the

[] Violence [] Adult language [] Other [=====]

area, whenever someone checks all 3, that only the last one actually gets inserted into the table.

i want that if all 3 are selected, all 3 go into the same 'warnings' column.

You want to combine all the values into ONE field in the database table? If that is the case then as I previously mentioned give each checkbox a distinct name then you would have to concantenate the values when inserting them:

INSERT INTO tablename(warningcolumn) values('$warning1." ".$warning2." ".$warning3');

Personally I wouldn't recommend that though. You should have a seperate column for each.

Spookster
09-05-2002, 03:54 AM
You know it might be beneficial to you since you are getting into server-side development with php and mysql if you learned a bit more about it. Here is a decent tutorial for beginners:

http://www.mysql.com/articles/ddws/