[UPHPU] Checkboxes and MySQL

Steve Dibb steve at wonkabar.org
Tue Nov 28 11:41:05 MST 2006


Nicholas Vettese wrote:
> This is something I have been working on for some time, and I can't get
> an answer from the 101 books, nor google on what I am doing wrong.
> Could be me, who knows.  :)
>  
> I have a form, and on that form are checkboxes.  I am trying to get the
> information from the users into the database.  I am not programmer, and
> loops make me dizzy, so I was hoping that you may have some information
> for me.  Here is my code:
>  
> <script>
> if (isset($_POST['submitted'])) { // Handle the form.
>  require_once('mysql_connect-local.php');
>  foreach ($test as $m) {
>   $query = "INSERT INTO movies (test) VALUES ('$m')";  
>   $result = mysql_query ($query) or trigger_error("Query: $query\n<br
> />MySQL Error: " . mysql_error());
>  }
> }
> ?>
> <h1>Checkbox Testing</h1>
> <p>Please tell us which type of movies you like</p>
> <form action="movies.php" method="post">
> <input type="checkbox" name="test[]" value="vintage" />Vintage<br />
> <input type="checkbox" name="test[]" value="sci-fi" />Scince Fiction<br
> />
> <input type="checkbox" name="test[]" value="documentary"
> />Documentary<br />
> <input type="checkbox" name="test[]" value="comedy" />Comedy<br />
> <input type="checkbox" name="test[]" value="family" />Family<br />
> <input type="checkbox" name="test[]" value="drama" />Drama<br />
> <input type="checkbox" name="test[]" value="adult" />adult<br />
>  
> <input type="submit" name="submit" value="Submit" />
> <input type="hidden" name="hidden" value="TRUE" />
> </form>
> </script>
>
>   
Do this:  Change the name to test[comedy] and the value to 1. Then, 
print out $_POST['test'] and youll have an array of the variables.  If 
they were checked, they will be set to 1.  If they are not, they will be 
non-existant.

Steve



More information about the UPHPU mailing list