简体   繁体   English

将复选框值存储在数据库中

[英]Store checkbox value in database

Below is code that I am using, when i submit the survey, arrrive and mode work but not trans/checkboxes, data comes back displayed as 'on' in table (survey) database (wdtlabwork), also the field is trans, same with the other two being mode n arrive in the database, all fields were type VARCHAR.下面是我正在使用的代码,当我提交调查、到达和模式工作但不是 trans/checkboxes 时,数据在表(调查)数据库(wdtlabwork)中返回显示为“on”,该字段也是 trans,与另外两个是模式 n 到达数据库,所有字段都是 VARCHAR 类型。 Apologies if formatting is bad i am new to site.抱歉,如果格式不好,我是网站新手。 NOTE: I want the data to come back as either car, train or bus but because its checkboxes if the user checked train and car i want the database to display train and car.注意:我希望数据以汽车、火车或公共汽车的形式返回,但因为如果用户检查了火车和汽车,它的复选框我希望数据库显示火车和汽车。

HTML HTML

<section id="content">
<form action="connect.php" method="post">
<div class="col-6 col-s-9">
  <h3>Available Transportation?</h3>
<input id="trans1" type="checkbox" name="trans[]"><label for ="trans1">Car</label>
<input id="trans2" type="checkbox" name="trans[]"><label for ="trans2">Train</label>
<input id="trans3" type="checkbox" name="trans[]"><label for ="trans3">Bus</label>
<h4>How do you intend to arrive at the Hostel?</h4>
<input id="arrive1" type="radio" value="car" name="arrive"><label for="arrive1">Car</label>
<input id="arrive2" type="radio" value="train" name="arrive"><label for="arrive2">Train</label>
<input id="arrive3" type="radio" value="bus" name="arrive"><label for="arrive3">Bus</label>
<h5>Preferred mode of transport?</h5>
<select name="mode">
<option selected hidden value="">Select Option</option>
<option value="car">Car</option>
<option value="train">Train</option>
<option value="bus">Bus</option>

</select><input type="submit" class="btn btn=primary"></div></section>

PHP PHP

<?php
$arrive = $_POST['arrive'];
$trans = $_POST['trans'];
$mode = $_POST['mode'];

$conn = new mysqli('localhost', 'root','','wdtlabwork');


if ($conn->connect_error){
    die('Connection Failed : '.$conn-> connect_error);
}else{
$stmt = $conn->prepare("insert into survey(trans, arrive, mode)
    values(?, ?, ?)");
$stmt->bind_param("sss",$trans, $arrive, $mode);
$stmt->execute();
echo "registration successfully...";
$stmt->close();
$conn->close();
}

?> ?>

Your input fields do not have value= attribute so they default to on as described here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Value您的输入字段没有value=属性,因此它们默认为on ,如下所述: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Value

To get the values in the POST you need to specify the values, for example:要获取 POST 中的值,您需要指定值,例如:

<input id="trans1" type="checkbox" name="trans[]" value ="car"><label for ="trans1">Car</label>
<input id="trans2" type="checkbox" name="trans[]" value ="train"><label for ="trans2">Train</label>
<input id="trans3" type="checkbox" name="trans[]" value ="bus"><label for ="trans3">Bus</label>

Then in your PHP you would receive an array of the selected elements in $_POST['trans'] .然后在您的 PHP 中,您将收到$_POST['trans']中选定元素的数组。 If you would like to join them ( not recommended ) to be saved in a single field in the database you can just use implode(',', $_POST['trans']) , for example:如果您想加入它们( 不推荐)以保存在数据库中的单个字段中,您可以使用implode(',', $_POST['trans']) ,例如:

$trans = implode(',', $_POST['trans']);
$arrive = $_POST['arrive'];
$mode = $_POST['mode'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM