Hi peeps, I need your help I have been struggling today trying to get a form working. I have created a form where data will needs to be filled in. The results from the form will be filled in two seperate tables and these forms have checkbox and radio buttons but does not want to save it into the tables.

First it only saves the data into the first table and not the second. and second of all the data is not saved as checkboxes when I view it in mysql or the readio buttons. I have also tryed changin the type in mysql to "ENUM" but i get errors when I save it. Can someone please help me? The code what I have created is below:

<?php 

if ($_POST['requestform']) { // this is saying if the submitForm button is press carry out the next if statement 

if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['contract']) || empty($_POST['deptmanager']) || empty($_POST['accounttype']) || empty($_POST['mailboxlist']) || empty($_POST['outlookmail']) || empty($_POST['distributionlist']) || empty($_POST['department']) || empty($_POST['hours'])) //this code is checking if the various text boxes are empty if they are print error message 
{ 
    echo 'You Must Enter All fields to submit request'; 
    echo '<br> '; 
    echo '<br> '; 
    if (isset($HTTP_REFERER)) {echo "<a href='$HTTP_REFERER'>BACK</a>"; } //this is php coding to create a hyperlink back button 
    else {echo "<a href='javascript:history.back()'>BACK</a>"; 
    } 

    exit; 
} 
    $conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); // this is the code for connection to the database created if cannot connect will send error message 
    mysql_select_db("joiners",$conn) or die("Could Not Select The Database"); 

     { 

        $sql = "INSERT INTO joinrequest (account_type,email_account,phone_login,telephone_ext,desktop,laptop,token,mob  ilephone,deskphone,blackberry,usbkey) VALUES ('','".$_POST['accounttype']."','".$_POST['phonelogin']."','".$_POST['telext']."','".$_POST['desktop']."','".$_POST['laptop']."','".$_POST['token']."','".$_POST['mobilephone']."','".$_POST['deskphone']."','".$_POST['blackberry']."','".$_POST['usbkey']."')"; //this part of the code runs the SQL Query of Inserting all the data gathered by each of the text boxes into each of the chosen attributes respectively 
        $query = mysql_query($sql,$conn) or die(mysql_error("Could Not Write Information To The Database")); 
        if (mysql_affected_rows($conn) == 0) { 
            echo 'This Client Was Not Added.'; 
        } else { 
            echo 'This Client Was Added Successfully';// if everything was successfully completed it will print this message if not it will print the error message above 
        } 

    }      
        { 

        $sql1 = "INSERT INTO user (first_name,last_name,join_date,department,contract,hours,dept_manager,mailbox  _list,distribution_list,hardware,software) VALUES ('','".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['joindate']."','".$_POST['department']."','".$_POST['contract']."','".$_POST['hours']."','".$_POST['deptmanager']."','".$_POST['mailboxlist']."','".$_POST['distributionlist']."','".$_POST['hardware']."','".$_POST['software']."',)"; //this part of the code runs the SQL Query of Inserting all the data gathered by each of the text boxes into each of the chosen attributes respectively 
        $query1 = mysql_query($sql1,$conn) or die(mysql_error("Could Not Write Information To The Database")); 
        if (mysql_affected_rows($conn) == 0) { 
            echo 'This Client Was Not Added.'; 
        } else { 
            echo 'This Client Was Added Successfully';// if everything was successfully completed it will print this message if not it will print the error message above 
        } 

    }   
   } 

?> 

<br>
<font color="#003399" face="Geneva, Arial, Helvetica, sans-serif"><strong>Employee
Information</strong></font><br>
<font face="Geneva, Arial, Helvetica, sans-serif">First Name: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="firstname">
<br>
Date(yyyy/mm/dd):
<input type="text" name="joindate">
</font><br>
<label><font face="Geneva, Arial, Helvetica, sans-serif">Permanent
<input type="radio" name="contract" value="Permanent">
</font></label>
<font face="Geneva, Arial, Helvetica, sans-serif">
<label> Contract
<input type="radio" name="contract" value="Contract">
</label>
</font><font face="Geneva, Arial, Helvetica, sans-serif"><br>
Department Manager:
<input type="text" name="deptmanager">
<br>
<br>
</font><font color="#003399" face="Geneva, Arial, Helvetica, sans-serif"><strong>Request<br>
</strong></font>
<label> <font face="Geneva, Arial, Helvetica, sans-serif"> New Account</font></label>
<font face="Geneva, Arial, Helvetica, sans-serif"></font>
<label><font face="Geneva, Arial, Helvetica, sans-serif">
<input type="radio" name="accounttype" value="New">
</font></label>
<font face="Geneva, Arial, Helvetica, sans-serif">
<label> Current Account</label>
<label>
<input type="radio" name="accounttype" value="Current">
</label>
</font><font face="Geneva, Arial, Helvetica, sans-serif"><br>
MailBox Listbr>
<textarea name="mailboxlist" cols="40" rows="5"></textarea>
</font><font face="Geneva, Arial, Helvetica, sans-serif"><br>
Outlook Mail: &nbsp;&nbsp; &nbsp;&nbsp;
<input type="checkbox" name="outlookmail" value="checkbox">
<br>
Distribution Listbr>
<textarea name="distributionlist" cols="40" rows="5">Landmark House</textarea>
<br>
Phone Login: &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="phonelogin" value="checkbox">
<br>
Telephone Ext: &nbsp;
<input type="checkbox" name="telext" value="checkbox">
</font><br>
<br>
</p>
</td>
<td width="370" valign="top"><p><br>
</p>
<p><font face="Geneva, Arial, Helvetica, sans-serif">Last Name: &nbsp;
<input type="text" name="lastname">
<br>
Department:
<input type="text" name="department">
<br>
</font>
<label> <font face="Geneva, Arial, Helvetica, sans-serif"> Temporary</font></label>
<font face="Geneva, Arial, Helvetica, sans-serif"></font>
<label><font face="Geneva, Arial, Helvetica, sans-serif">
<input type="radio" name="hours" value="Temporary">
</font></label>
<font face="Geneva, Arial, Helvetica, sans-serif">
<label> </label>
Flexibility
<label>
<input type="radio" name="hours" value="Flexibility">
</label>
<label> </label>
Part Time
<label>
<input type="radio" name="hours" value="PartTime">
</label>
</font><br>

<br>
</p></td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="80" rowspan="3" bgcolor="dcdcdc">&nbsp;</td>
<td rowspan="3" bgcolor="003399">&nbsp;</td>
<td rowspan="3">&nbsp;</td>
<td height="164" valign="top">
<p><font color="#003399" face="Geneva, Arial, Helvetica, sans-serif"><strong>Hardware
Request </strong></font><font face="Geneva, Arial, Helvetica, sans-serif"><br>
Desktop: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="desktop" value="checkbox">
<br>
Laptop: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="laptop" value="checkbox">
<br>
Token: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="token" value="checkbox">
<br>
Mobile Phone</font>: <font face="Geneva, Arial, Helvetica, sans-serif">
&nbsp;
<input type="checkbox" name="mobilephone" value="checkbox">
</font><br>
<font face="Geneva, Arial, Helvetica, sans-serif">Desk Phone: &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="deskphone" value="checkbox">
<br>
Blakcberry</font>: <font face="Geneva, Arial, Helvetica, sans-serif">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="blackberry" value="checkbox">
</font><br>
<font face="Geneva, Arial, Helvetica, sans-serif">USB Key: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="usbkey" value="checkbox">
<br>
</font> </p></td>
<td valign="top"> <p><font color="#003399" face="Geneva, Arial, Helvetica, sans-serif"><strong>
Software Request</strong></font><br>
<font face="Geneva, Arial, Helvetica, sans-serif"><strong>Default System
contains/strong><br>
MS Office 2003, CASE, Adobe Reader, PDF Writer, Visio Viewer, AL Zip (WinZip),
WINIS/SPIN and Right fax.<br>
</font></p></td>
<td rowspan="3">&nbsp;</td>
</tr>
<tr>
<td height="193" valign="top"> <p><font face="Geneva, Arial, Helvetica, sans-serif">
Other please specify/font><br>
<font face="Geneva, Arial, Helvetica, sans-serif">
<textarea name="hardware" cols="40" rows="5"></textarea>
</font></p>
<p><font face="Geneva, Arial, Helvetica, sans-serif"> </font></p>
<p><font face="Geneva, Arial, Helvetica, sans-serif"></font></p></td>
<td valign="top"><font face="Geneva, Arial, Helvetica, sans-serif">Other please
specifybr>

<textarea name="software" cols="40" rows="5"></textarea>
</font></td>
</tr>
<tr>
<td height="88" colspan="2"><div align="center">
<input type="submit" name="requestform" value="Request">
</div></td>

</tr>
<tr>
<td height="19" bgcolor="003399">&nbsp;</td>
<td bgcolor="003399">&nbsp;</td>
<td bgcolor="003399">&nbsp;</td>
<td colspan="2" bgcolor="003399">&nbsp;</td>
<td bgcolor="003399">&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>

    Check boxes like radio buttons have the same name but different values, what I did see by scanning your html is showing chckboxes with a value of check box so if you clicked any of them with this value it would baturally have a value of checkbox. The checkboxes are arrays with the same name for a group or related checkboxes. Take for example a group of checkbozes that allows people to indicate any or all dirnks that they might enjoy

    <input type="checkbox" name ="drink[]" value ="cola">Cola
    <input type="checkbox" name ="drink[]" value ="tea">Tea
    <input type="checkbox" name ="drink[]" value ="milk">Milk
    <input type="checkbox" name ="drink[]" value ="Coffee">Coffee
    <input type="checkbox" name ="drink[]" value ="beer">Beer
    <input type="checkbox" name ="drink[]" value ="wine">Wine

    If the user checked Cola you would find that value in $POST['drink'][0]
    or you could use associative arrays like this

    <input type="checkbox" name ="drink[cola]" value ="cola">Cola
    <input type="checkbox" name ="drink[tea]" value ="tea">Tea
    <input type="checkbox" name ="drink[milk]" value ="milk">Milk
    <input type="checkbox" name ="drink[coffee]" value ="coffee">Coffee
    <input type="checkbox" name ="drink[beer]" value ="beer">Beer
    <input type="checkbox" name ="drink[wine]" value ="wine">Wine

    Then you would find the value for Cola by using $_POST['drink'][cola]
    So you need to change your form a little and also how you process the checkbox values in your php processing file.

      Will I jus need to change the array in html code? Also in mysql what will be the type? Because at the moment it is varchar and there is not an option for boolean. I am new user to this so I am not absolutely sure what to do here please guide me. thanks 🙂

        A boolean would take much less space and be easier to deal with instead of assigning words just a 0 or one so set up the checkbox values as a boolean with 1 meaning yes or true, and 0 meaning no or false. Then you could if you wish have a default of 0 since unless checked of course the value would be 1, the problem there is since any or all could be checked then you might want to use the associative arrays and run a test that if checked would assign 1 and if not assign 0 like this

        $cola =($_POST['drink'][cola]== 'cola'? '1': '0');
        $coffee =($_POST['drink'][coffee]== 'coffee'? '1': '0');
        $tea =($_POST['drink'][tea]== 'tea'? '1': '0');

        Hey I am still not really awake but maybe you can see how I am assigning a value to the checkboxes in this case if your form actually had these boxes and you submitted it and the user checked cola and coffee but not tea then $coffee would have a value of 1 and $cola would also but $tea would be set to 0. Does this help any?

          Write a Reply...