如何将复选框中的多个值添加到mysql数据库中

时间:2017-01-06 12:18:03

标签: php mysql

我已经创建了一个包含两组复选框的表单,并希望将它们存储在MySQL数据库中,但是当我发布到数据库时,所有数据都按预期传递,例如日期,文本和单选按钮,除了两个文本框。如果我查看数据库中存储文本框值的列,它只会说"数组"并没有实际值。

这是我处理帖子请求的代码:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


$name = $_POST['input4'];
$datedone = $_POST['date'];
$projectnumber = $_POST['input1'];
$area   = $_POST['input2'];
$donebefore = $_POST['radio9'];
$changesmade    = $_POST['radio8'];
$safeaccess = $_POST['radio11'];
$electrical = $_POST['radio5'];
$machineguarding    = $_POST['radio6'];
$correctequipment   = $_POST['radio4'];
$sds    = $_POST['radio3'];
$controltoxic   = $_POST['radio1'];
$ppe    = $_POST['radio2'];
$hazard = $_POST['checkbox'];
$otherhazards   = $_POST['input3'];
$controlofhazards   = $_POST['checkbox1'];
$monitor    = $_POST['radio12'];
$comments   = $_POST['input'];

$sql = "INSERT INTO hira (Name, TodayDate, ProjectNumber, Area, DoneBefore, HaveChangesMade, SafeAccess, ElectricalEquipment, MachineGuarding, CorrectEquipment, SDS, ControlToxic, PPE, Hazard, OtherHazard, ControlHazard, MonitorProcess, AdditionalComments) VALUES ('$name','$datedone','$projectnumber','$area','$donebefore','$changesmade','$safeaccess','$electrical','$machineguarding','$correctequipment','$sds','$controltoxic','$ppe','$hazard','$otherhazards','$controlofhazards','$monitor','$comments')";

if ($conn->query($sql) === TRUE) {
    echo "Thank you for completing the Hira form";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

我在这里做错了什么。我希望文本框的值以逗号分隔,即&#34; Noise,Dust&#34; 这可能吗? 提前致谢

2 个答案:

答案 0 :(得分:0)

您可以使用implode功能

$checkbox = implode(",",$_POST['checkbox']);

答案 1 :(得分:0)

你可以使用implode函数插入数组使用,分隔字符串

$values = implode(",",$_POST['name']);

您可以使用explode函数将字符串检索到数组

$values = explode(",",$values);