I’m a little stuck as to why I keep the error: " Error : (1048) Column ‘GC’ cannot be null".
What I’m trying to do is validate and insert data into database, where each check-box has it’s own column in the db.
Previously I had all the the check boxes going into the same column (they were just separated by commas).
Below is the the form:
<form name="emailform" id="emailform" method="post" action ="<?php echo $_SERVER['PHP_SELF']; ?> ">
<div>
<ul class="e-flyer-check-area">
<li> <input type="checkbox" name="GC" value="GC"/> General Contractor</li>
<li> <input type="checkbox" name="HB" value="HB"/> Home Builder </li>
<li> <input type="checkbox" name="RR" value="RR"/> Residental Roofer </li>
<li> <input type="checkbox" name="CR" value="CR"/> Commercial Roofer </li>
<li> <input type="checkbox" name="RS" value="RS"/> Restoration Contractor </li>
<li> <input type="checkbox" name="RN" value="RN"/> Renovator </li>
<li> <input type="checkbox" name="SC" value="SC"/> Stucco Contractor </li>
<li> <input type="checkbox" name="CC" value="CC"/> Ceiling Contractor </li>
</ul>
<ul class="e-flyer-check-area">
<li> <input type="checkbox" name="DC" value="DC"/> Drywall Contractor</li>
<li> <input type="checkbox" name="DB" value="DB"/> Drywall Boarding <span>Contractor</span></li>
<li> <input type="checkbox" name="TC" value="TC"/> Drywall Texture <span>Contractor</span></li>
<li> <input type="checkbox" name="DF" value="DF"/> Drywall Finishing <span>Contractor</span></li>
<li> <input type="checkbox" name="FC" value="FC"/> Framing Contractor </li>
<li> <input type="checkbox" name="IN" value="IN"/> Insulator </li>
<li> <input type="checkbox" name="SC" value="SC"/> Masonry Contractor </li>
<li> <input type="checkbox" name="WP" value="WP"/> Water Proofer </li>
</ul>
</div>
<div>
<ul class="e-flyer-submit-area">
<li class="divider2"></li>
<li> <input type="text" name="name" id="name" placeholder="Name*" value="<?php echo $_POST['name']; ?>" /> </li>
<li> <input type="text" name="email" id="email" placeholder="E-mail*" value="<?php echo $_POST['email']; ?>" /></li>
<li> <input type="checkbox" name="opt" value="<?php echo 'opt-in';?>" > Yes, I agree to opt In!* </li>
<li> <input type="submit" name="submit" class="submit btn btn-lt-grey" value="Submit"/> </li>
</ul>
</div>
</form>
The PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$opt = $_POST['opt'];
$GC = $_POST['GC'];
//$HB = $_POST['HB'];
?>
<div id="e-flyer-validate">
<ul>
<?php
if(isset($_POST['submit'])){
$booValidateOK = 1;
$strValidationMessage = "";
//name
if(strlen($name) < 3){
// $boolValidateOK = 0;
// $strValidationMessage .= "\n <li>Please fill in a name.</li>";
}
//validate e-mail
$emailValidate = validate_email($email);
if(!$emailValidate){
$boolValidateOK = 0;
$strValidationMessage .= "\n <li>please fill in a proper e-mail.</li>";
}
//validate opt-in
if(!$opt){
$boolValidateOK = 0;
$strValidationMessage .= "\n <li>Please check opt in.</li>";
}
//validate checkboxes
//names of posted checkboxes
/*if(!empty($_POST['info'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['info']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['info'] as $selected) {
echo "<p>".$selected ."</p>";
}
}else{
echo"Please Select at least one Category";
} */
}//end of submit
if($booValidateOK == 1){
//$info = ($_POST['info[]']);
//$pieces = implode(", ",$info);
if(($email !="" && $opt != "")){
//mysqli query
$results = "INSERT INTO Emails (name, email, opt, GC) VALUES (?, ?, ?, ?)";
$statement = $conn->prepare($results);
//bind parameters for markers, where (s = string, i = integer, d = double, b = blob)
$statement->bind_param('ssss', $name, $email, $opt, $GC);
//execute
if($statement->execute()) {
//print 'Success';
}else{
die('Error : ('. $conn->errno .') '. $conn->error);
}
}//end of insert
}//close here
function validate_email( $email ){
$email = trim( $email ); # removes whitespace
if(!empty($email) ):
// validate email address syntax
if( preg_match('/^[a-z0-9\_\.]+@[a-z0-9\-]+\.[a-z]+\.?[a-z]{1,4}$/i', $email, $match) ):
return strtolower($match[0]); # valid!
endif;
endif;
return false; # NOT valid!
}// close validate_email function
echo $strValidationMessage;
?>
</ul>
</div>
If someone could point me in the right direction, that would be great!