0

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

<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”。表 = id、cat1、cat2、cat3

4

1 回答 1

0

创建这个 php 代码

<?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)');
    }
}
?>
于 2013-09-28T02:29:53.757 回答