mglover88 Posted April 18, 2011 Share Posted April 18, 2011 Hi, I have a registration form built in dreamweaver designed to insert data into my database through PHP, with some of the fields in my form optional to the user. My understanding is that I should set these 'optional fields' to NULL in mysql in order to be able to proceed with the registration should the user choose not to enter any data. This appears to work fine and the website moves on as required upon the form being submitted. However, should the user choose to enter something into one of these fields marked NULL in mysql the data is lost and when I look at the inserted row it simply says NULL, regardless of what data the user has entered in the form. If I go the other way and mark the field NOT NULL then the data appears correctly, only for the user to get an error message saying 'column cannot be null' should they decide to leave it blank. I am a newbie to the world of PHP and mysql so am probably missing something simple, but have been told that the problem is probably in my PHP coding by someone on a different forum. Any help would be much appreciated, I think the problem could lie in the following coding: <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $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']); } Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/234077-problem-inserting-row-with-empty-values-to-mysql-via-php/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.