March 21st, 2000, 09:33 PM
Ok, I know I keep running into careless errors but I have a mental block with this code and can't seem to find the problem.
I'm trying to process a form by sending information to the database. None of the fields are required, all are declared as NOT NULL. If I leave a field blank, no problems. But I leave a checkbox field blank, my query won't run through. And the only way my data is inserted into the database is if I select ALL the options in the checkboxes. I haven't put them into an array yet -- will do that later -- right now just concerned with why I can't leave a checkbox blank.
Here's the code:
if (isset($ID) && isset($First_Name) && isset($Exam1) && isset($Exam2) && isset($Exam3) && isset($Exam4) && isset($Exam5) && isset($Exam6) && isset($Exam7))
echo "IS SET";
mysql_connect("i-20experts.com", "username", "password");
$query = "INSERT INTO Applicants VALUES ('$ID', '$First_Name', '$Exam1', '$Exam2', '$Exam3', '$Exam4', '$Exam5', '$Exam6', '$Exam7')";
$result = mysql_db_query("host", $query);
<input type="Hidden" name="ID">
<input type="Text" name="First_Name" size="20" maxlength="60">
Mark any of the following exams you have taken:
<input type="checkbox" name="Exam1" value="TOEFL"> TOEFL
<input type="checkbox" name="Exam2" value="SAT I"> SAT I
<input type="checkbox" name="Exam3" value="SAT II"> SAT II
<input type="checkbox" name="Exam4" value="ACT"> ACT
<input type="checkbox" name="Exam5" value="GRE"> GRE
<input type="checkbox" name="Exam6" value="GMAT"> GMAT
<input type="checkbox" name="Exam7" value="Other"> Other
I've named them all as separate fields temporarily -- they will be in an array later on. But I can't seem top find why I can't leave one blank when they are already declared Not Null in the schema. If I select all of them, I get my desired output of "IS SET" and the data is inserted into the database. If I leave even one blank, nothing goes through.
Someone point me to the right direction again please...
March 22nd, 2000, 12:25 AM
I believe with a checkbox it has to be clicked for the variable to even be set. In other words, it's not even a NULL without being checked and unchecked.
March 22nd, 2000, 01:37 AM
Actually I was wrong. I don't think it has a value even when it's checked and unchecked, but there is a solution, posted in the Devshed HTML/DHTML forum under:
"Possible to put a value for non-checked checkboxes? "
Another answer is not to even check in PHP whether those particular variables are set at all. (maybe the simplest thing, but think about it some)
March 22nd, 2000, 11:31 AM
So I don't have to list them in the ISSET section at all to check if the variable is set? The last time I left out a variable from the ISSET section, my query didn't run through at all. How would I go about doing that?
March 22nd, 2000, 11:32 PM
I suppose you could do something like:
$Exam1 = 0
(! being the NOT operator...)
Then run your query as you coded before.
March 23rd, 2000, 11:59 AM
Thanks, that did the trick!