Forum Moderators: coopster

Message Too Old, No Replies

Inserting values into DataBase

Inserting several values in one field via checkboxes

         

black sabbath

9:54 am on Apr 26, 2004 (gmt 0)

10+ Year Member



Dear members,
I am a new to php scripting. I have the problem with inserting several values in one field, from checkboxes (in the field appears only the value of last ticked checkbox). here is a code I wrote:

<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("test", $db);
$sql = "INSERT INTO form_checker VALUES(NULL, '$name', '$des_field')";
mysql_query($sql);
?>

<form action="example.php" method="Post">
Name: <input type="text" size="30" name="name"><br>
Age: <input type="checkbox" name="des_field" value="age"><br>
Sex: <input type="checkbox" name="des_field" value="sex"><br>
Location: <input type="checkbox" name="des_field" value="location"><br>
<input type="Submit" value="Submit">
</form>

Any help would be appreciated.

Thanks in advance

Netizen

11:40 am on Apr 26, 2004 (gmt 0)

10+ Year Member



I would suggest you put something like

Age: <input type="checkbox" name="des_field[age]" value="age"><br>
Sex: <input type="checkbox" name="des_field[sex]" value="sex"><br>
Location: <input type="checkbox" name="des_field[location]" value="location"><br>

into your HTML. When you fill in a form where three fields have the same name then only the last value in the form will get sent if all of them are checked. What exactly are you try to achieve on the form?

In the PHP, given the HTML above, you can extract the information by using $des_field['age'], $des_field['sex'], etc.

black sabbath

8:51 am on Apr 27, 2004 (gmt 0)

10+ Year Member



Many thanks Netizen,

I tried the way you offered but it won't work. In my web site there will be a list of choices (offers). And the users will have to choose at least three items from the list. I am trying to put these data, chosen from list in one field of the table using checkboxes.

I want to offer users multiple choice, and the data to be stored in one field.

Thanks again, I really appriciate your help

David

Netizen

9:59 am on Apr 27, 2004 (gmt 0)

10+ Year Member



If you want to store the array values in one field just do

$string=implode(',',$des_field);

and then insert $string in the appropriate column. implode [php.net] will take an array and join the parts together with the specified delimiter, in this case a comma.

I hope that helps.