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

Dani AI

Generated

, two checkboxes for a yes/no question let users tick both. Either use one radio group per question or a single checkbox (treat missing as 0). Also avoid the old mysql_* API; it was removed in PHP 7. Use PDO or mysqli with prepared statements. If you want each question stored as its own row (what you asked for), give every question a stable key and post them as an array.

HTML names to aim for (example for one item):
<input type="radio" name="answers[backdoor]" value="1"> Yes
<input type="radio" name="answers[backdoor]" value="0"> No
Repeat for laptop_monitor and app_logs. is right that names must differ; grouping as answers[...] keeps processing simple.

Here is a compact, safe insert that writes one row per answer:

-- table design: one row per question answered
CREATE TABLE audit_answers (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  question_key VARCHAR(32) NOT NULL,
  answer TINYINT(1) NOT NULL,  -- 1=yes, 0=no
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
<?php
// insert.php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') exit;

$pdo = new PDO(
  'mysql:host=localhost;dbname=yourdb;charset=utf8mb4',
  'db_user','db_pass',
  [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

$questions = ['backdoor','laptop_monitor','app_logs']; // keys used in your form
$pdo->beginTransaction();
$stmt = $pdo->prepare('INSERT INTO audit_answers (question_key, answer) VALUES (?, ?)');

foreach ($questions as $q) {
  $val = isset($_POST['answers'][$q]) ? (int)$_POST['answers'][$q] : 0; // default to No
  $stmt->execute([$q, $val]);
}
$pdo->commit();
echo 'Saved.';

Tip: If you truly need checkboxes (multi-select), use name="items[]" and insert each selected value; for boolean questions, prefer radios or a single checkbox.

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

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.