1
votes

I have created the following checkbox form to display hobbies. The array enters into the database in the same cell which is perfect however, the following errors appear on the page before i have even submitted. ( I am new to this and any help would be greatly appreciated :))

Undefined index: hobbies in (line 29) Warning: implode(): Invalid arguments passed in (line 29)

The code:

$checkbox=implode(',', $_POST['hobbies']);

if(isset($_POST['Submit'])){

  //for($i=0; $i<sizeof($hobbies);$i++){

   $query=queryMysql("INSERT INTO hobbies  VALUES ('$user', '" .$checkbox. "')");

    mysql_query($query) or die(mysql_error());
    echo "record is inserted";
  }




echo <<<_END

<body>
<form method='post' action='hobbies.php' >
<input type="checkbox" name="hobbies[]" value="reading">Reading<br>
<input type="checkbox" name="hobbies[]" value="cycling">Cycling<br>
<input type="checkbox" name="hobbies[]" value="swimming">Swimming<br>
<input type="checkbox" name="hobbies[]" value="running">Running<br>
<input type="submit" name='Submit' value='Submit'/></form>
</body>
</html>




_END;
?>
3
when you load the page initially, there is no such variable $_POST['hobbies'], so put this into your if statement. - mitkosoft
The question has already been answered below, so on a side note: you wouldn't let strangers into your house, and yet you seem to be ok with letting them into your database. "Yeah, guys, supply anything you want! If your name happens to be blahblah' OR 1/*, then so be it!" - Eihwaz
put this line $checkbox=implode(',', $_POST['hobbies']); inside if condition. - jagad89
Instead of concatenating values with a comma (where you'd have to add escaping, btw) you should take a look at serialization of arrays instead. - arkascha

3 Answers

0
votes

Maybe put this line : $checkbox=implode(',', $_POST['hobbies']); in your if(isset($_POST['Submit'])){ } condition.

0
votes

Try This code, I hope it will work :-

if(isset($_POST['Submit'])){


  $checkbox=implode(",", $_POST['hobbies']);

   $query=queryMysql("INSERT INTO hobbies  VALUES ('$user', '$checkbox')");

    mysql_query($query) or die(mysql_error());
    echo "record is inserted";
  }


echo <<<_END
0
votes
I think you need to more customization like this 


        if(isset($_POST['Submit'])){

        if(is_array( $_POST['hobbies']) && count( $_POST['hobbies']) >0){
        $checkbox=implode(',', $_POST['hobbies']);

          //for($i=0; $i<sizeof($hobbies);$i++){

           $query=queryMysql("INSERT INTO hobbies  VALUES ('$user', '" .$checkbox. "')");

            mysql_query($query) or die(mysql_error());
            echo "record is inserted";
          }
        echo <<<_END
    } ?>
        <body>
        <form method='post' action='hobbies.php' >
        <input type="checkbox" name="hobbies[]" value="reading">Reading<br>
        <input type="checkbox" name="hobbies[]" value="cycling">Cycling<br>
        <input type="checkbox" name="hobbies[]" value="swimming">Swimming<br>
        <input type="checkbox" name="hobbies[]" value="running">Running<br>
        <input type="submit" name='Submit' value='Submit'/></form>
        </body>
        </html>



<?php        
        _END;
        ?>