I have created similar forms like this on multiple occassions but for some reason this is not happening. I had this working but have lost what I did!! doh.
Here is a form 0 notice the category and consequential sub category drop down menus. The idea is you look up a category and the sub categories are looked up when a category s chosen. This works fine.
I have three tables:
CREATE TABLE sp_profile (
spprofile_id int(11) NOT NULL auto_increment,
username varchar(30) default NULL,
organisation varchar(50) default NULL,
rate varchar(50) default NULL,
last_entered timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
profiletitle varchar(50) default NULL,
cat varchar(50) default NULL,
PRIMARY KEY (spprofile_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
--
-- Table structure for table sp_subcategory
CREATE TABLE sp_subcategory (
username varchar(10) collate latin1_general_ci NOT NULL default '',
subcategory_id smallint(5) unsigned NOT NULL default '0',
last_updated timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (username,subcategory_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
--
-- Table structure for table subcategory
CREATE TABLE subcategory (
subcategory varchar(200) default NULL,
cat_id int(2) default NULL,
subcategory_id int(11) NOT NULL auto_increment,
PRIMARY KEY (subcategory_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=180 ;
My form is like so:
<input type="hidden" name="username" value="<?=$_SESSION['username']?>" />
</strong>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='sp_addprofilev2.php?cat=' + val ;
}
</script></td>
</tr>
<tr>
<td>Choose category </td>
<td> <?php
/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
To read more on register_global visit.
http://www.plus2net.com/php_tutorial/register-globals.php
*/
@$cat=$_GET['cat']; // Use this line or below line if register_global is off
//@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off
///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category");
///////////// End of query for first list box////////////
/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory");
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); }
////////// end of query for second subcategory drop down list box ///////////////////////////
echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example action=dd-check.php////
////////// Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
////////////////// This will end the first drop down list ///////////
////////// Starting of second drop downlist /////////
echo "<select name=\"subcategory[]\" size=\"5\" multiple=\"multiple\">";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
////////////////// This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
?>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Profile title </td>
<td><input name="profiletitle" type="text" id="profiletitle" value="<?=$row['profiletitle']?>" size="40" /></td>
</tr>
<tr>
<td width="131">Organisation</td>
<td width="459"><input name="organisation" type="text" id="organisation" value="<?=$row['organisation']?>" size="40" /></td>
</tr>
<tr>
<td>Rate</td>
<td><select name="rate" id="rate">
<?php
$values = array('£1', '£2', '£3');
foreach($values as $val)
{
printf("<option%s>$val</option>\n", ($val == $row['rate']) ? " selected" : "");
}
?>
</select></td>
</tr>
<tr>
<td>Click submit </td>
<td><label>
<input name="submit" type="submit" id="submit" value="Submit" />
</label></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
and my formhandler:
?php
error_reporting(E_ALL);
include("include/session.php");
include("common_functions.php");
$username = $_SESSION['username'];
if(isset($_POST['submit'])){
include("connect.php");
$_SESSION['form1_data'] = $_POST;
// build main query from form
$sql = "INSERT INTO sp_profile SET spprofile_id = NULL ";
foreach ($_SESSION['form1_data'] as $col =>$val ){
if($col <> 'submit'){
if ($col=='subcategory'){
//ignore and process later below for seperate table insert
}
else
$sql .=", $col = '$val' ";
}
}
//////////////////////////////////
mysql_query($sql, $connection) or die(mysql_error());
}
//////////////debugging///////////////////
$sql = "SELECT * FROM sp_profile WHERE username = '".$username."'";
$result = mysql_query($sql);
// if we have rows, fetch them & prepopulate the form
if(mysql_num_rows($result) > 0) {
echo 'sp profile insert successful';
}
else {
echo'sp profile not happenning';
}
//update sp_subcategory table
foreach($_POST['subcategory'] as $value){
$sql = 'INSERT INTO sp_subcategory (`username`, `subcategory_id`) VALUES ("'.$username.'", "'.$value.'")
ON DUPLICATE KEY UPDATE last_updated = NOW()';
$result = mysql_query($sql, $connection) or die(mysql_error());
}
echo '<br><br>';
/////debugging//////// ///////////////////////////////////////////
$sql = "SELECT * FROM sp_subcategory WHERE username = '".$username."'";
$result = mysql_query($sql);
// if we have rows, fetch them & prepopulate the form
if(mysql_num_rows($result) > 1) {
echo 'spsubcategories insert successful';
}
else {
echo'spsubcategories insert not happenning';
}
?>
the sub categories are not inserting but the data for sp_profile is. I cannot see why though.
the error message is:
sp profile insert successful
Warning: Invalid argument supplied for foreach() in /var/www/vhosts/mindseyemidlands.co.uk/httpdocs/icommission/spaddprofile_handlerv2.php on line 41
spsubcategories insert not happening
many thanks in advance