Forum Moderators: coopster
I setup a form to insert data into mysql database. It's working great for the most part. However, I have a drop down menu I would like to use for one of the data points. It won't take that data. It says "Column 'sector' cannot be null". It's just this drop down, without it things work fine. I'll include code here and cross my fingers someone will have mercy and help me. :)
?php require_once('Connections/connTest.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 ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO ideas (idea_title, firstname, lastname, email, idea_summary, keywords, sector, theme) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['idea_title'], "text"),
GetSQLValueString($_POST['firstname'], "text"),
GetSQLValueString($_POST['lastname'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['idea_summary'], "text"),
GetSQLValueString($_POST['keywords'], "text"),
GetSQLValueString($_POST['sector'], "text"),
GetSQLValueString($_POST['theme'], "text"));
mysql_select_db($database_connTest, $connTest);
$Result1 = mysql_query($insertSQL, $connTest) or die(mysql_error());
$insertGoTo = "thankyou.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?><!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>Untitled Document</title>
</head>
<body>
<p>Submit Your Idea</p>
<p> </p>
<p> </p>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Your Idea:</td>
<td><input type="text" name="idea_title" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">First Name:</td>
<td><input type="text" name="firstname" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Last Name:</td>
<td><input type="text" name="lastname" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Email:</td>
<td><input type="text" name="email" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right" valign="top">Idea Summary:</td>
<td><textarea name="idea_summary" cols="50" rows="5"></textarea>
</td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Keywords:</td>
<td><input type="text" name="keywords" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Sector:</td>
<td><select name="sector">
<option value="" >Energy, Food & Water</option>
<option value="" >Economics & Business</option>
<option value="" >Communications & Media</option>
<option value="" >Environment & Infrastructure</option>
<option value="" >Art & Culture</option>
<option value="" >Health & Wellness</option>
<option value="" >Law & Justice</option>
<option value="" >Security</option>
<option value="" >Learning & Education</option>
<option value="" >Governance</option>
<option value="" >Science & Technology</option>
<option value="" >Spirituality</option>
<option value="" >Hub</option>
</select>
</td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Theme:</td>
<td><select name="theme">
<option value="" >Greening Big Systems</option>
<option value="" >Creating Resilient Communities</option>
<option value="" >Developing concsious leadership</option>
<option value="" >Creating a new mythos (story)</option>
<option value="" >Connecting Sectors & Strategies</option>
</select>
</td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>
<p> </p>
</body>
</html>
Column 'sector' cannot be null
That's a MySQL error that probably can be probably avoided by running this before other queries:
mysql_query('SET sql_mode = ""'); You may wish to read the manual page on SQL modes [dev.mysql.com] before using that snippet.