php - mysql : how to submit 2 sets of checkbox in two different column in a table

标签 php html mysql checkbox

我有两组带有不同复选框选项的 div :

<div id="mobile_device" style="display:none;">
<font size="3"><b>Select the accessories :</b></font>
</br></br>
    <input type="checkbox" name="mobile[]" value="charger"/>  Charger &nbsp;&nbsp; &nbsp; <input type="checkbox" name="mobile[]" value="case"/> Case &nbsp;&nbsp;&nbsp;
    <input type="checkbox" name="mobile[]" value="headset"/> Headset &nbsp;&nbsp; &nbsp; <input type="checkbox" name="mobile[]" value="box"/> Box&nbsp;&nbsp; &nbsp; 
    <input type="checkbox" name="mobile[]" value="usb"/> USB Cable &nbsp;&nbsp; &nbsp; <input type="checkbox" name="mobile[]" value="sim"/> SIM<br/>
</div>

div id="desktop_device" style="display:none;">
<font size="3"><b>Select the accessories :</b></font>
</br></br>
<input type="checkbox" name="desktop[]" value="adaptor"/>  Adaptor &nbsp;&nbsp; &nbsp; <input type="checkbox" name="desktop[]" value="privacy"/> Privacy Screen &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="desktop[]" value="mouse"/> Mouse &nbsp;&nbsp; &nbsp; <input type="checkbox" name="desktop[]" value="keyboard"/> Keyboard&nbsp;&nbsp; &nbsp; 
<input type="checkbox" name="desktop[]" value="connector"/> Desktop Connector <br/><br/>
</div>

现在我试图在数据库的两列中提交一组复选框值。

if (isset($_POST['submit']))
    { 
        // get form data, making sure it is valid
        $type = $_POST['type'];
        $name = $_POST['name'];
        $serial = $_POST['serial'];
        $imei = $_POST['imei'];
        $mobile = implode(",", $_POST['mobile']);
        $desktop = implode(",", $_POST['desktop']);
        $chargerno = $_POST['chargerno'];
        $connectorno = $_POST['connectorno'];
        $monitor = $_POST['monitor'];
        $monitorno = $_POST['monitorno'];       



            // save the data to the database
            $sql3= "INSERT INTO devices (type, name, serial, imei, mobile, desktop, chargerno, connectorno, monitor, monitorno) VALUES ('$type', '$name', '$serial', '$imei', '$mobile', $desktop, $chargerno, $connectorno, $monitor, $monitorno)";
            mysqli_query($mysqli,$sql3) or die(mysqli_error($mysqli));
            }   

但是我收到了类似“字段列表”中的未知列“适配器”的错误。

如何将两个 div 中选定的复选框值提交到两个不同的列中。

最佳答案

$desktop 必须作为字符串传递,因此:

$sql3= "INSERT INTO devices (type, name, serial, imei, mobile, desktop, chargerno, connectorno, monitor, monitorno) VALUES ('$type', '$name', '$serial', '$imei', '$mobile', '$desktop', $chargerno, $connectorno, $monitor, $monitorno)";

关于php - mysql : how to submit 2 sets of checkbox in two different column in a table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21122304/

相关文章:

javascript - 尝试使用 js、jquery 和 PHP 上传图片时出错

php - 使用 Zend Db 的嵌套选择

php - Silex - app->json() 将整数数据作为字符串返回

php - 将 JSON 数据导入 MySQL

javascript - 为什么这段代码不显示背景图片?

php - 如何将多个复选框选择值放在SQL表的单独列中?

jquery - 如何使 jQuery 生成的文本居中

html - 在很多元素中使用 !important 可以吗?如果不是如何解决这个问题

mysql - sql 连接表,其中 1 列有逗号

mysql - 将查询从 NOT IN 更改为 NOt EXISTS