将多个复选框项添加到数据库

时间:2014-10-15 20:12:08

标签: php mysql

我是php / mysql的新手,我学到了很多东西。我的复选框遇到了麻烦。 我看了,我遇到的一切对我来说都没有意义。

我正在做一个管理员可以添加用户的网站,并且有一些复选框可以说出用户感兴趣的内容。(可以选择多个。

实施例 对什么运动感兴趣。 (这些是用户可以选择的复选框)

  • 棒球
  • 足球
  • 曲棍球

等等

如何将选项存储在数据库中?

这是我到目前为止所拥有的。

HTML

<div class="col-md-8">
<label style="margin-right:10px; width:130px"><input name="interested[]" type="checkbox" value="a6"><span class="cats">Baseball</span></label>
<label style="margin-right:10px; width:130px"><input name="interested[]" type="checkbox" value="a6"><span class="cats">Football</span></label>
<label style="margin-right:10px; width:130px"><input name="interested[]" type="checkbox" value="a6"><span class="cats">Hockey</span></label>
</div>

PHP

<?php
//error_reporting(E_ALL);
//ini_set("display_errors", 1);

// Pick up the form data and assign it to variables
$id = @$_POST['id'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$altemail = $_POST['altemail'];
$notes = $_POST['notes'];
$company = $_POST['company'];
$address = $_POST['address'];
$home = $_POST['home'];
$cell = $_POST['cell'];
$telephone = $_POST['telephone'];
$category = $_POST['category']; 
$usertype = $_POST['usertype']; 
$assigned = $_POST['assigned']; 
$othercat = $_POST['othercat']; 
$interested=$_POST['interested'];



//Get data in local variable

$id = @$_POST['id'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$altemail = $_POST['altemail'];
$notes = $_POST['notes'];
$company = $_POST['company'];
$address = $_POST['address'];
$home = $_POST['home'];
$cell = $_POST['cell'];
$telephone = $_POST['telephone'];
$category = $_POST['category']; 
$usertype = $_POST['usertype'];
$assigned = $_POST['assigned']; 
$othercat = $_POST['othercat'];     
$interested=$_POST['interested'];

// You have to loop through the array of checked box values ...
$interested="";
foreach($interested as $entry){
$interested .= $entry.",";
}


if ($fname=="" || $email=="")
{
echo "All fields must be entered, hit back button and re-enter information";
}else{



$query="INSERT INTO users(`id`, `fname`, `lname`, `email`, `notes`,`company`,`address`,`cell`,`home`,`telephone`,`category`,`usertype`,`assigned`,`altemail`,`othercat`,`interested`) VALUES('$id','$fname','$lname','$email','$notes','$company','$address','$telephone','$category','$usertype','$assigned','$altemail','$othercat','$cell','$home','$interested')";

就像我提到的,我是PHP / Mysql的新手。我现在只做了大约8天。我已经采取了很多方法,但这让我很难过。

任何帮助将不胜感激。不是试图让自己失望,而是与我所看到的所有其他人都无法理解,请为我愚​​蠢。

提前致谢。

0 个答案:

没有答案