I've got a page that builds a bit of a form from my database, giving people a whole load of choices with checkboxes. I'm now having trouble getting the results back into the database. here's my code. I've stripped everything out of the page apart from the bit I'm on about. Here's the code. hope it makes sense to someone!:
<?php require_once('../Connections/act.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}
mysql_select_db($database_act, $act);
$query_category = "SELECT * FROM directory_cat";
$category = mysql_query($query_category, $act) or die(mysql_error());
$row_category = mysql_fetch_assoc($category);
$totalRows_category = mysql_num_rows($category);
do {
if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form1")) {
$cat=$row_category['cat_id'];
$insertSQL = sprintf("INSERT INTO dir_rel_cat (cat_id, user_id) VALUES (%s, %s)",
GetSQLValueString(isset($HTTP_POST_VARS['category']) ? "true" : "", "defined","$cat","0"),
GetSQLValueString($HTTP_POST_VARS['hiddenField'], "int"));
mysql_select_db($database_act, $act);
$Result1 = mysql_query($insertSQL, $act) or die(mysql_error());
}
}
while ($row_category = mysql_fetch_assoc($category));
mysql_select_db($database_act, $act);
$query_category = "SELECT * FROM directory_cat";
$category = mysql_query($query_category, $act) or die(mysql_error());
$row_category = mysql_fetch_assoc($category);
$totalRows_category = mysql_num_rows($category);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ACT online</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<p align="center">Please select the categories that describe
your work:
<input name="hiddenField" type="hidden" value="<?php echo $id; ?>">
<br>
<?php do {
$cat=$row_category['cat_id'];?>
<input type="checkbox" name="category" value="<?php echo $cat; ?>">
<?php echo $row_category['category']; ?>
<?php } while ($row_category = mysql_fetch_assoc($category)); ?>
</p>
<p align="center"> <br>
<br>
<input type="submit" name="Submit" value="finished!">
<input type="hidden" name="MM_insert" value="form1"></p>
</form>
</div>
</body>
</html>
<?php
mysql_free_result($category);
?>
my code to insert the data is a bastardised version of the code generated by dreamweaver MX it just inserts all the values into the database - as if all the boxes were ticked.