PHP code for insert checkbox value into spesific column on mysql with select form

被刻印的时光 ゝ 提交于 2019-12-18 09:53:21

问题


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">

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


回答1:


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)');
    }
}
?>


来源:https://stackoverflow.com/questions/19062410/php-code-for-insert-checkbox-value-into-spesific-column-on-mysql-with-select-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!