Oops! I realized after I'd written my previous reply that what I wrote
works only if one or more of the check boxes is checked! If the user
chooses none of them then there's nothing to join() and an error is
thrown. What I did was added
if (empty($category))
$category =array_fill(1,1,"");
before the part of the function that generates the INSERT that is sent
to the MySQL database. The above is just a silly way of assigning an
empty value to the $category array. First it checks if $category has
any values (if any of the check boxes were checked) and if not then it
fills the array with the value "" once, in the first position. I'm
sure there are more graceful ways of achieving this, but as I've
written several times, my PHP is somewhat lacking and so I tend to try
to find a workaround for my problems that isn't too complicated and
then go with it if I can't get it to break or throw an error.
For those keeping score at home, the page code in its entirety now
looks like this:
<?php require_once('Connections/PoProjDBs.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
$theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) :
$theValue;
$theValue = function_exists("mysql_real_escape_string") ?
mysql_real_escape_string($theValue) : mysql_escape_string($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 = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if (empty($category))
$category =array_fill(1,1,"");
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO news (news_title, news_subtitle,
news_author, news_body, news_date, articlePlace, category) VALUES (%s,
%s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['title'], "text"),
GetSQLValueString($_POST['subtitle'], "text"),
GetSQLValueString($_POST['author'], "text"),
GetSQLValueString(ereg_replace(' +', ' ',
($_POST['body'])) , "text"),
GetSQLValueString($_POST['news_date'], "date"),
GetSQLValueString($_POST['placement'], "text"),
//joins the values of the array $category from the check boxes into a
string
GETSQLValueString(join(',',$category), "text"));
mysql_select_db($database_PoProjDBs, $PoProjDBs);
$Result1 = mysql_query($insertSQL, $PoProjDBs) or die(mysql_error());
$insertGoTo = "article_list.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<?php
include("fckeditor/fckeditor.php") ;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Article Insert</title>
<script src="SpryAssets/SpryValidationTextField.js"
type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet"
type="text/css" />
</head>
<body>
<form id="form1" name="form1" method="POST" action="<?php echo
$editFormAction; ?>">
<table border="1" width="100%">
<tr>
<td valign="top">Title</td>
<td valign="top"><span id="sprytextfield1">
<input name="title" type="text" id="title" size="50" />
<span class="textfieldRequiredMsg">A value is
required.</span></span></td>
</tr>
<tr>
<td valign="top">Sub-Title</td>
<td valign="top"><input name="subtitle" type="text" size="50"
/></td>
</tr>
<tr>
<td valign="top">Author</td>
<td valign="top"><input name="author" type="text" size="50" /></td>
</tr>
<tr>
<td colspan="2" valign="top"><?php
$oFCKeditor = new FCKeditor('body') ;
$oFCKeditor->BasePath = 'fckeditor/';
$oFCKeditor->Value = 'Enter the news copy here.';
$oFCKeditor->Width = '80%' ;
$oFCKeditor->Height = '400' ;
$oFCKeditor->ToolbarSet = 'PoProj';
$oFCKeditor->Create() ;
?></td>
</tr>
<tr>
<td colspan="2" valign="top">Where will this article appear?
<select name="placement">
<option value="interiorOnly" selected="selected">Interior
Pages Only</option>
<option value="article1">Home Page, Top Article</option>
<option value="article2">Home Page, Middle Article</option>
<option value="article3">Home Page, Bottom Article</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" valign="top">Check all categories that apply:
<label>
<input type="checkbox" name="category[]" value="news" />
News</label>
<label>
<input type="checkbox" name="category[]" value="reviews" />
Review</label>
<label>
<input type="checkbox" name="category[]" value="ppapers" />
Project Papers</label>
<label>
<input type="checkbox" name="category[]" value="interviews" />
Interview</label>
<label>
<input type="checkbox" name="category[]" value="newsletter" />
Newsletter Articles</label>
<label>
<input type="checkbox" name="category[]" value="poems" />
Poems</label>
<label>
<input type="checkbox" name="category[]" value="articles" />
Articles</label>
<label>
<input type="checkbox" name="category[]" value="essays" />
Essays</label>
</td>
</tr>
</table>
<?php
// this splits up the date for the "month/year" field
$date= date("Y-m-d");
?>
<input type="hidden" name="news_date" value="<?php echo $date ?>">
<input type="hidden" name="MM_insert" value="form1" />
<input type="submit" name="submit" id="submit" value="Submit" />
<input type="reset" name="reset" id="reset" value="Reset" />
</form>
<script type="text/javascript">
<!--
var sprytextfield1 = new
Spry.Widget.ValidationTextField("sprytextfield1");
//-->
</script>
</body>
</html>