I've created an html form that has text fields, drop down lists and checkboxes. The php programming started out well, but has now gotten to be more complex than I anticipated. So, I created a simpler form to learn off of before implementing it to the more difficult form. This is probably really basic, but I have no experience with arrays. I have two questions that will help get me going here:
Here is the code I am testing with to learn:
HTML:
<FORM METHOD="POST" ACTION="colortesting.php">
<P>
</P>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="38%">
<tr>
<td width="24%">
<INPUT TYPE="checkbox" NAME="color[1]"
VALUE="red">
Red</td>
<td width="76%"><select size="1" name="DD[]">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
</select></td>
</tr>
<tr>
<td width="24%">
<INPUT TYPE="checkbox" NAME="color[2]"
VALUE="blue">
Blue</td>
<td width="76%"><select size="1" name="DD[]">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
</select></td>
</tr>
<tr>
<td width="24%">
<INPUT TYPE="checkbox" NAME="color[3]"
VALUE="green">
Green</td>
<td width="76%"><select size="1" name="DD[]">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
</select></td>
</tr>
<tr>
<td width="24%">
<INPUT TYPE="checkbox" NAME="color[4]"
VALUE="yellow">
Yellow </td>
<td width="76%"><select size="1" name="DD[]">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
</select></td>
</tr>
</table> <p>
<input type="submit" value="Submit" name="B1"> </p>
</FORM>
PHP:
<?php
$color = $POST['color'];
$self = $SERVER['PHP_SELF'];
$DD = $_POST['DD'];
#connect to mysql
$conn=@mysql_connect( "localhost", "root", "password" )
or die( "Error connecting to MySQL" );
#select specified database
$rs = @mysql_select_db( "test", $conn )
or die( "Error connecting to db" );
if(isset($POST['color'])) {
$array = $POST['color'];
$string = implode($array,",");
echo $string;
}
$sql = "INSERT into colors (color) VALUES ('$string')";
$rs = mysql_query( $sql, $conn )
or die( "Could not execute query" );
?>
Here are two questions:
First, I've been playing with implode. It gets the data into the db, but all as one string. How do I get each checkbox selection into its own row in the db?
Second, how do I link the checkbox with the correct drop down list? For instance, if someone selects the "red" checkbox, and "one" from the drop down list next to it, how do I store that in the database so that they are related to each other?
I've seen lots of examples with arrays where you hard code the data in, but not very many examples where the data is dependent on what the user selects. This is making me difficult to get the correct syntax when doing my statements....
Thanks for any help - I really struggle with programming, but love it at the same time!
Debbie