使用选择表单将复选框值插入到 mysql 上的特定列的 PHP 代码

2021-12-23 00:00:00 checkbox php mysql

我需要帮助 php 代码来识别我的复选框值插入到一些特定的列中,我的表单是这样的.

I need help php code to identify my checkbox value insert in to some spesific coloumn, my form like this.

<input type="checkbox" name="A" Value="Animal">Animal <br/>
<input type="checkbox" name="B" Value="Ball">Ball <br/>
<input type="checkbox" name="C" Value="Champ">Champ <br/>

<label for="select">Category</label>
<select name="select" size="1">
<option value="Category 1">Category 1</option>
<option value="Category 2">Category 2</option>
<option value="Category 3">Category 3</option>
</select>
<br/>
<input type="submit" name="Submit" value="Submit">

我需要如果选择类别 1 并提交复选框值将插入到表列cat1"中,我选择类别 2 并提交复选框值将插入到表列cat2"中.table = id, cat1, cat2, cat3

i need if select category 1 and make submit the checkbox value will insert in to table column "cat1" and i select category 2 and make submit the checkbox value will insert in to table column "cat2". table = id, cat1, cat2, cat3

推荐答案

创建这个 php 代码

Create this php code

<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('myTable');
$val = array();
if (isset($_POST['A']))
    $val[] = $_POST['A'];
if (isset($_POST['B']))
    $val[] = $_POST['B'];
if (isset($_POST['C']))
    $val[] = $_POST['C'];
$value = implode(', ', $val);
if (isset($_POST['select'])) {
    $col = $_POST['select'];
    if ($col == 'Category 1') {
        $query = mysql_query('INSERT INTO myTable(cat1) VALUES($value)');
    }
    else if ($col == 'Category 2') {
        $query = mysql_query('INSERT INTO myTable(cat2) VALUES($value)');
    }
    else if ($col == 'Category 3') {
        $query = mysql_query('INSERT INTO myTable(cat3) VALUES($value)');
    }
}
?>

相关文章