Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with Array

Status
Not open for further replies.

nvdp

Technical User
Nov 13, 2002
2
US
I am using PHP to take data from an HTML input form to a MYSQL database. I have an issue with one of my fields. The field is 'Language'.

Here is what I am after: I need to have multiple 'Language' checkboxes, so that the user can check all the Languages they speak (could often be more than 1). For example, let's say the user selects 'Spanish' and 'Italian'. I would then like to store this value in my MYSQL database as 'Spanish, Italian'.

Below is my code as it stands today:

HTML Code:
<input name="AG_LANGUAGE[]" type="checkbox" value="Spanish" /> Spanish
<input name="AG_LANGUAGE[]" type="checkbox" value="Italian" /> Italian

PHP Code:
<?php
$language = (!empty($_POST['AG_LANGUAGE'])? implode(',',$_POST['AG_LANGUAGE'])."\n" : '');
?>

The problem I'm having is that the input data is being stored in my Language table field as 'Array'. Is there a way to store the actual values in the field instead (ex. 'Spanish, Italian')? If not, how do I extract the actual values using SQL? Writing a simple SQL query produces the below result:

select ag_language
from ag_profile

'Array'

Thanks in advance for any help.
 
Thanks feherke for the quick reply.

I'm pretty sure I used the $language value. But I'll double check later tonight to be sure.

Any other thoughts or ideas?
 
Hi

Nope. For now, that was my best idea. Or worst...

For more probably you will have to post some more code. For example the [tt]mysql_query()[/tt] which puts the data into the database. And related commands if any, for example if you are composing the SQL statement elsewhere.

Feherke.
 
i believe that not all clients send a value to the server from a checkbox (other than a 1).

it is better, in my opinion to structure your checkboxes like this
Code:
$languages = array("spanish", "english","french");
foreach ($languages as $language){
 echo "<input type=\"checkbox\" name=\"checkbox[$language]\"> $language<br/>";
}

and then process them like this

Code:
foreach($languages as $language){
 if(isset($_POST['checkbox'][$language])){
   echo "he speaks $language<br/>";
   // do something with the db here
 } else {
   echo "he does not speak $language<br/>";
 }
}
 
Hi

jpadie said:
i believe that not all clients send a value to the server from a checkbox (other than a 1).
Correct. But hitting that bug would result a warning and the $language would still have correct string value ( a single separator ).

Otherwise your solution is certainly better. Otherwise a script kiddie could send a list of 2007++ languages, or whatever quantity would be necessary to do something nasty with the database.

Feherke.
 
Not to hijack a thread but...

jpadie said:
i believe that not all clients send a value to the server from a checkbox (other than a 1).

I'm shocked; I have never run into this problem. Do you know of any specific browsers which behave this way?

The value attribute of a checkbox is required by the HTML specs and if no value is given the client is suppose to send the value 'on'. I know browsers don't ever fully comply with specs but it seems like such a simple thing to adhere to.


-- -- -- -- -- -- -- --

If you give someone a program, you will frustrate them for a day
but if you teach them how to program, you will frustrate them for a lifetime.
 
sorry, itshim: i don't remember the specifics. for the longest time i've disregarded the value attribute because of this issue and have developed work around habits that are now rather entrenched. it may even be that all current browsers play nicely and I'm still in the dark ages.
 
Code:
$arr = $_POST['AG_LANGUAGE'];
$num = count($arr);
for($i = 0; $i > $num; $i++){
/* insert into DB */
echo $arr[$i];
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top