Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8323681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:47:40+00:00 2026-06-08T23:47:40+00:00

how to post check box values to different fields of mysql db.Ple look at

  • 0

how to post check box values to different fields of mysql db.Ple look at the code.In my code i have 18 options to select.i have a table of 18 fields.i just want to put these values in respective columns.Here is the code.

    <?php
    if(!empty($_POST['subjects'])) {
        foreach($_POST['subjects'] as $check) {
                echo $check."<br>"; 
        SQL query ???
        }
    }
    ?>

<form action="check.php" method="post">
<input type="checkbox" name="subjects[]" value="PakistanStudies" />
<input type="checkbox" name="subjects[]" value="Islamiat" />
<input type="checkbox" name="subjects[]" value="Urdu(SyllabusA)" />
<input type="checkbox" name="subjects[]" value="Urdu(SyllabusB)" />
<input type="checkbox" name="subjects[]" value="Mathematics" />
<input type="checkbox" name="subjects[]" value="AdMathematics" />
<input type="checkbox" name="subjects[]" value="Sociology" />
<input type="checkbox" name="subjects[]" value="ReligiousStudies" />
<input type="checkbox" name="subjects[]" value="EnglishLanguage" />
<input type="checkbox" name="subjects[]" value="EnglishLiterature" />
<input type="checkbox" name="subjects[]" value="Chemistry" />
<input type="checkbox" name="subjects[]" value="Physics" />
<input type="checkbox" name="subjects[]" value="PrinciplesofAccounting" />
<input type="checkbox" name="subjects[]" value="Economics" />
<input type="checkbox" name="subjects[]" value="BusinessStudies" />
<input type="checkbox" name="subjects[]" value="Biology" />
<input type="checkbox" name="subjects[]" value="Statistics" />
<input type="checkbox" name="subjects[]" value="World History" />
<input type="submit" />
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:47:42+00:00Added an answer on June 8, 2026 at 11:47 pm

    To connect to an SQL database via PHP, you can use the MySQLi Extension. First, make sure you enable the extension in the PHP.ini file and restart your web server so that the changes take affect.

    You should find this line in the PHP.ini:

    ;extension=php_mysqli.dll
    

    and simply change it to:

    extension=php_mysqli.dll
    

    Please refer to the documentation on how to use the MySQLi extension. But a quick mock example of connecting to a server and inserting data is as followed:

    <?php
    $mysqli = new mysqli($servername, $username, $password, $database);
    
    // Ensure connection
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }
    
    if ($mysqli->query($sqlCommabnd) === TRUE) {
        // Query was successful!
    }
    
    // Disconnect
    $mysqli->close();
    ?>
    

    Make sure you assign values to the servername, username, password, database and sqlCommand variables.

    I am unsure if I have already answered your question but just to be sure; your SQL statement will look something like:
    INSERT INTO table_name VALUES (val1, val2, val3…)
    and note that you can specify the columns explicitly by placing them before the VALUES keyword in parenthesis like so:
    INSERT INTO table_name (col1, col2, col3…) VALUES (val1, val2, val3…)

    Finally; to make sure everything goes smoothly for you. Your HTML form should be on a page with the action set to another (or perhaps the same) page. When the Submit event is fired, the page will redirect (or refresh). So make sure that your action is pointing to the page with all your PHP for this to work.

    If you are wondering what the POST data looks for check boxes. Try the following:
    print_r($_POST[“subjects”]);
    That should give you an idea of the structure of the array.

    Please let me know if you have any further problems, I’d be happy to answer them.

    EDIT – To help answer after comments

    Note: I’m assuming that the columns are Boolean types for these check boxes in the following situation.

    To insert or alter a field in the database where the value of the check box is the column name; try consructing the following SQL statement (altering it where needed):

    <?php
    $columns = "";
    $values = "";
    foreach($_POST["subjects"] as $checkedValue)
    {
        $columns .= $checkedValue . ", ";
        $values .= "true, ";
    }
    
    $sqlCommand = "INSERT INTO table (".substr($columns,0,-2).") VALUES (".substr($values,0,-2).");";
    
    echo $sqlCommand;
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get values of a multiple select check box using request.POST['xzy'] as
I have the following code. I want to add/remove check-box values to an array
I am trying to post an array of check box ids to an action
I found this post that shows how to pass multiple check box selections to
I have an HTML form: <form action='process.php' method='post'> <input type='checkbox' name='check_box_1' /> Check me!<br>
I have the following $_POST function to check if the fields of 'start', 'middle'
I have to insert roles for user by using check box but for inserting
I have my front end script which has the following jQuery code: $.get(/backend/post.php, {
I have a form which have five buttons and several checkboxes holding different values.
I have a form, depending on the selected value of my <select> box I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.