Hi. I'm new in PHP Mysql , please help me with this situation :
i had this php code

<form action="insert.php" method="post">
Check locked backdoor<br />
<input type="checkbox" name="Check[]" value="A" />Yes
<input type="checkbox" name="Check[]" value="B" />No    <br />
Laptops handover monitor<br />
<input type="checkbox" name="Check[]" value="A" />Yes
<input type="checkbox" name="Check[]" value="B" />No<br />
Check logs from App Servers<br />
<input type="checkbox" name="Check[]" value="A" />Yes
<input type="checkbox" name="Check[]" value="B" />No<br />
<input type="submit" name="formSubmit" value="Submit" />
</form>

I want to store data in checkboxes to mysql database ,please help me with code to insert values to database. Each value checked above is separate in table.
Thank you guys so much for your help

Recommended Answers

All 3 Replies

First off you might wanna make each of the names differnt since the user can pick multiple check boxes at the same time!!!

also make sure the php code goes first!! before any html
This code should work but no guarrantees (I dont have access to the php files from this pc)
and create your tables first in phpmyadmin

    CREATE TABLE tabName (
    col1 char(1)
    -- add any other collumns you like here 
    ); 

All code below goes in your php script!!

    <?php
    //puts a connection link inside  a variable 
    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    //checks to see if connection statement works, if not then you get an error message dont you!?!?! 
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }


    //pulls the values out of the checkboxes dawg!!



    //checks to see if at least one of the check boxes has been clicked if not then the user is returned with a nice informative message! :)
    if(isset(Check[1]) || isset(Check[2]) || isset(Check[3])) 
      {
         //some code here 
         if(isset(check[1]))
            {
              $checkBox1 = $_POST[Check[1]];  
              $sql1 = "insert into tabName (col1) values ($checkBox1);"; 
            }
         if(isset(check[2]))
            {
              $checkBox2 = $_POST[Check[2]]; 
              $sql2 = "insert into tabName(col1) values($checkBox2);";
            }
        if(isset(check[3]))
           {
             $checkBox3 = $_POST[Check[3]];
             $sql3 = "insert into tabName(col1) values ($checkBox3);";       
           }
      }

      //checks to see which sql statements to run!! 

      if(isset($checkBox1))
       {
         mysql_query($sql1);     
       }
      if(isset($checkBox2))
       {
        mysql_query($sql2); 
       }
    if(isset($checkBox3))
       {
         mysql_query($sql3); 
       }   

    else {
      echo "<h1 style='color:red;'> You have to click on something </h1>";  
      }

      //this goes at the end of the code (Well if you so choose to kill the connection you dont have too, will do it once the session has ended!!! 
    mysql_close($link);

    ?>

    <!-- php code goes before any html code --> 

    <html>
    <head><title> Being cool and helping others </title>  </head>
    <body>
    <form>
    Check to see if you locked the backdoor<br />
    <input type="checkbox" id="Check[1]" name="Check[1]" value="A" />Yes
    <input type="checkbox" id="Check[1]" "Check[1]" value="B" />No <br />
    Laptops handover monitor (whatever this means) <br />
    <input type="checkbox" id="Check[2]" name="Check[2]" value="A" />Yes
    <input type="checkbox" id="Check[2]" name= "Check[2]" value="B" />No<br />
    Check logs from App Servers<br />
    <input type="checkbox" id="Check[3]" name="Check[3]" value="A" />Yes
    <input type="checkbox" id="Check[3]" name= "Check[3]" value="B" />No<br />
    <input type="submit" name="formSubmit" id='submit'  value="Submit" />
    </form>

    </body>
    </html>
commented: Thank you for help , very helpful though still had some errors :) +0

What if I want to create a student course
registration form

@thrizy Start a new thread and include all the information covering it and how far you've got. Include any code that you want help with or as much as you have. People here will help you but noone wants to do it all for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.