Forum Moderators: coopster

Message Too Old, No Replies

data insert into mysql : Please HelP

data insert into mysql : Please HelP

         

mirozake

7:12 pm on Jun 30, 2008 (gmt 0)

10+ Year Member



Ok. First, disclaimer, I'm very new to PHP. :) So, thank you extra.

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>&nbsp;</p>
<p>&nbsp;</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">&nbsp;</td>
<td><input type="submit" value="Insert record" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
<p>&nbsp; </p>
</body>
</html>

RonPK

7:30 pm on Jun 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

mirozake

8:10 pm on Jun 30, 2008 (gmt 0)

10+ Year Member



Thanks for the advice. It didn't fix it. It's something to do with the fact that these are drop downs. Almost as if it doesn't register the selections as data. ANything more? Also, just to be sure, I put your line beginnin of second <?php, right before if (!function_exists...etc... Let me know if it needs to go somewhere else.

mirozake

3:07 am on Jul 1, 2008 (gmt 0)

10+ Year Member



ok. i'm ready to bribe. $10 bucks to whoever helps me and solves my problem. :)

RonPK

8:13 am on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem may be that you're trying to insert NULL into a column that has been defined NOT NULL (which off course is a wild guess since we don't know the column definition). Is there any particular reason for that?

mikeyb

10:09 am on Jul 1, 2008 (gmt 0)

10+ Year Member



The problem is with your drop-down, none of the options have any values:
<option value="" >Energy, Food & Water</option>

You need to put a value in that will be inserted into your database.

You'll then have the same problem for 'theme'

Cheers,
Mike