如何同时将复选框值和文本字段数据发布到mysql数据库中

I need help posting data from HTML forms to a MySql database, especially checkboxes, I don't have a problem connecting to the database but I have a problem posting checkbox values and text field data at the same time, please assist me with the PHP code.

I have the following code.

<form action="action_page.php">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
</fieldset>
<fieldset>
<legend>Course:</legend>
<input type="checkbox" name="french" value="French"> French
<br>
<input type="checkbox" name="german" value="German"> German 
<br>
<input type="checkbox" name="spanish" value="Spanish"> Spanish 
</fieldset>
<input type="submit" value="Submit">
</form>

Post data are available in a global variable called $_POST.

To get values from a HTML form, after submit you just have to use this variable.

If for example french check-box is checked you will have:

$_POST['french'] = "french"

You have to see whether the user has checked that check-box or not. So just use isset() function:

if (isset($_POST['french'])) {
    //french check-box is checked.
} else {
    //french check-box is not checked.
}

And for text-fields you will have:

$_POST['firstname'] = "Mickey"

Also you have to change <form action="action_page.php"> to <form action="action_page.php" method="POST"> because HTML uses GET method by default.

Change name of checkbox so you can post multiple value.

<form action="action_page.php" method="post">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input typ`enter code here`e="text" name="lastname" value="Mouse">
<br><br>
</fieldset>
<fieldset>
<legend>Course:</legend>
<input type="checkbox" name="course[]" value="French"> French
<br>
<input type="checkbox" name="course[]" value="German"> German 
<br>
<input type="checkbox" name="course[]" value="Spanish"> Spanish 
</fieldset>
<input type="submit" value="Submit">
</form>

In your php code check posted data from view

 var_dump($_POST)

and use mysql query to insert or update.

Here is what you need

index.php

<form action="action_page.php" method="post">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" id="firstname" value="">
<br>
Last name:<br>
<input type="text" name="lastname" id="lastname" value="">
<br><br>
</fieldset>
<fieldset>
<legend>Course:</legend>
<input type="checkbox" name="language" id="language" value="French"> French
<br>
<input type="checkbox" name="language" id="language" value="German"> German 
<br>
<input type="checkbox" name="language" id="language" value="Spanish"> Spanish 
</fieldset>
<input type="submit" id="submit"name="submit">
</form>

action_page.php

$dbhost = "localhost";
$dbusername = "YOUR DB USERNAME";
$dbpassword = "YOUR DB PASSWORD";
$dbname = "YOUR DB NAME";

if(isset($_POST['submit'])){

    $first_name = $_POST['firstname']; // required

    $last_name = $_POST['lastname']; // required

    $language = $_POST['language']; // required

    $connection = mysql_connect($dbhost, $dbusername, $dbpassword) or die('Could not connect');

    $db = mysql_select_db($dbname);

    $sql = "INSERT INTO `user`(`first_name`, `last_name`, `language`) VALUES ('$first_name','$last_name','$language')";

    $new = mysql_query($sql);

    if($new){

        echo("<br>Input data is succeed");

    } else{

        echo("<br>Input data is fail");

    }

}

Sql Table

   CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `language` varchar(255) NOT NULL
   ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

Put the sql database under any db name in the sql and that's it!