I have this registration form which has 4 check boxes. I would like to report the selection ( 1 or more ) and insert their value as 1 in my database under the preferred selection. The below is my html code
<tr>
<td>SMS Centers *</td>
<td colspan="2"><table width="210" border="0" class="greennbold">
<tr>
<td width="22" align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="mumbai" value="1"></td>
<td width="188">MUMBAI</td>
</tr>
<tr>
<td align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="pune" value="1"></td>
<td>PUNE</td>
</tr>
<tr>
<td align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="banglore" value="1"></td>
<td>BANGALORE</td>
</tr>
<tr>
<td align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="mysore" value="1"></td>
<td>MYSORE (August)</td>
</tr>
</table></td>
</tr>
<tr>
Below will be my php code.
<?php
include ('database_connection.php');
$mumbai=isset($POST['checkbox']) ? 1 : 0;
$pune=isset($POST['checkbox']) ? 1 : 0;
$banglore=isset($POST['checkbox']) ? 1 : 0;
$mysore=isset($POST['checkbox']) ? 1 : 0;
if (isset($POST['formsubmitted'])) {
$error = array();//Declare An Array to store any error message
if (empty($POST['mobileno'])) {//if no name has been supplied
$error[] = 'Please Enter a Mobile Number ';//add to array "error"
} else {
$mobileno = $POST['mobileno'];//else assign it a variable
}
if (empty($POST['fname'])) {//if no name has been supplied
$error[] = 'Please Enter a First name ';//add to array "error"
} else {
$fname = $POST['fname'];//else assign it a variable
}
if (empty($POST['lname'])) {//if no name has been supplied
$error[] = 'Please Enter a Last name ';//add to array "error"
} else {
$lname = $POST['lname'];//else assign it a variable
}
if (empty($POST['email'])) {
$error[] = 'Please Enter your Email ';
} else {
if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA- Z0-9\._-]+)+$/", $_POST['email'])) {
//regular expression for email validation
$email = $_POST['email'];
} else {
$error[] = 'Your EMail Address is invalid ';
}
}
if (empty($POST['passwd1'])) {
$error[] = 'Please Enter Your Password ';
} else {
$passwd1 = $POST['passwd1'];
}
if (empty($POST['passwd2'])) {
$error[] = 'Please Verify Your Password ';
} else {
$passwd2 = $POST['passwd2'];
}
if (empty($error)) //send to Database if there's no error '
{ //If everything's OK...
// Make sure the mobile no is available:
$query_verify_mobileno = "SELECT * FROM userdtls WHERE mobileno = '$mobileno'";
$result_verify_mobileno = mysqli_query($dbc, $query_verify_mobileno);
if (!$result_verify_mobileno)
{//if the Query Failed ,similar to if($result_verify_mobileno==false)
echo ' Database Error Occured ';
}
if (mysqli_num_rows($result_verify_mobileno) == 0) { // IF no previous user is using this number .
// Create a unique activation code:
//$activation = md5(uniqid(rand(), true));
$query_insert_user = "INSERT INTO userdtls ( `mobileno`, `pass`, `fname`, `lname`, `email`, `MUM`, `PUN`, `BNG`, `MYS` ) VALUES ( '".$mobileno."', '".$passwd1."', '".$fname."', '".$lname."', '".$email."', '".$mumbai."', '".$pune."', '".$banglore."', '".$mysore."' )";
$result_insert_user = mysqli_query($dbc, $query_insert_user);
if (!$result_insert_user) {
echo 'Query Failed ';
}
if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.
// Finish the page:
echo '<div class="success">Thank you for registering! </div>';
} else { // If it did not run OK.
echo '<div class="errormsgbox">You could not be registered due to a system error. We apologize for any inconvenience.</div>';
}
} else { // The mobile number is not available.
echo '<div class="errormsgbox" >That mobile number has already been registered. </div>';
}
} else {//If the "error" array contains error msg , display them
echo '<div class="errormsgbox"> <ol>';
foreach ($error as $key => $values) {
echo ' <li>'.$values.'</li>';
}
echo '</ol></div>';
}
mysqli_close($dbc);//Close the DB Connection
} // End of the main Submit conditional.