Hello
can someone please help me, I am trying to add information to a database with checkbox. I have every thing working expect for the checkbox. I am fairly new to the php/sql side of web coding. Anyway I want to add information to the one field of a database, in this case fruit. When I click submit it show the information as array, but I want it to be displayed as the value, eg banana, apple not as Array.

<form action="script/test.php" method="post">
<table>
<TD><input type="checkbox" name="fruit[]" id='fruit[]' value='banana'> 
  banana</TD>
<TD><input type="checkbox" name="fruit[]" id='fruit[]' value='apple'> 
  Apple</TD>
</tr>
<tr>
<TD></TD>
<TD><input type="checkbox" name="fruit[]" id='fruit[]' value='pine'> 
  Pineapple</TD>
<TD><input type="checkbox" name="fruit[]" id='fruit[]' value='other'> Other</TD>
<tr></tr>
<tr>
</TABLE></form

Php code

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

$sql="INSERT INTO food (Fruit)
VALUES
('$_POST[fruit]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "information has been added";

mysql_close($con)
?>

hi

you need to use implode() php function for convert string
for e.g

$fruits=$_POST[fruit];
$fruits_str=implode(',',$fruits);//it seprates value by comma
$fruits_str=implode(' ',$fruits);//it seprates value by space

I hope this function help you to solve problem
Thanks

Welcome mccs.

This is your first post and it is very impressive.

You missed Submit Button and also "fruits" is an array and its elements should be written into a table.

<?php
       $cmd=$_REQUEST["cmd"];
      // If submit button is pressed 
      if(isset($cmd)) {
       $con = mysql_connect("localhost","root","");
           if (!$con) {
                  die('Could not connect: ' . mysql_error());
            }

        mysql_select_db("test", $con);
        $fruit=$_REQUEST["fruit"];   // array of checked box values

        foreach($fruit as $i)  {
             $sql="INSERT INTO food (Fruit)  VALUES ('$i')";
                 if (!mysql_query($sql,$con)) {
                           die('Error: ' . mysql_error());
                   }
               }
               echo "information has been added";
           mysql_close($con);
     }
?> 

<form action="script/test.php" method="post">
<table>
 <tr>
  <td>
     <input type="checkbox" name="fruit[]" id='fruit[]'  value='banana'> 
  banana
 </td>
 <tr>
 <td><input type="checkbox" name="fruit[]" id='fruit[]' value='apple'> 
  Apple</td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name="fruit[]" id='fruit[]' value='pine'> 
  Pineapple</td>
<td><input type="checkbox" name="fruit[]" id='fruit[]' value='other'> Other</td>
<tr></tr>
<tr>
 <tr>
    <td>
       <input type="submit" name="cmd" value="Submit"/>
   </td>
 </tr>
</table>
</form>

Thanks for your help guys really appreciate the help and fast reply.

sorry for the messy post, it was driving me crazy, and I need to get away from the computer, I accidentally left out the submit button, due to having the checkbox half way in my code.

but thanks again for the help.

This question has already been answered. Start a new discussion instead.