在mySQL表中的三列中插入多个复选框值

时间:2015-03-20 00:17:45

标签: php mysql

我在数据库表中有三列,并希望从复选框中将0到3个值(主题)添加到列中。

HTML:

<ul class="checkbox" id="topicarea">
                                <li><input type="checkbox" name="topic[]" value="AG">Agriculture<br /></li>
                                <li><input type="checkbox" name="topic[]" value="ART">Art<br /></li>
                                <li><input type="checkbox" name="topic[]" value="BIO">Biological Sciences<br /></li>
                                <li><input type="checkbox" name="topic[]" value="BUS">Business<br /></li>
                                <li><input type="checkbox" name="topic[]" value="COM">Communication<br /></li>
                                <li><input type="checkbox" name="topic[]" value="IT">Information Technology<br /></li>
                                <li><input type="checkbox" name="topic[]" value="EDU">Education<br /></li>
                                <li><input type="checkbox" name="topic[]" value="ENGR">Engineering<br /></li>
                                <li><input type="checkbox" name="topic[]" value="ENVS">Environmental Science<br /></li>
                                <li><input type="checkbox" name="topic[]" value="HE">Health<br /></li>
                                <li><input type="checkbox" name="topic[]" value="LANGLIT">Language and Literature<br /></li>
                                <li><input type="checkbox" name="topic[]" value="LAW">Law<br /></li>
                                <li><input type="checkbox" name="topic[]" value="PHR">Philosophy and Religion<br /></li>
                                <li><input type="checkbox" name="topic[]" value="PHYS">Physical Science<br /></li>
                                <li><input type="checkbox" name="topic[]" value="PSY">Psychology and Counseling<br /></li>
                                <li><input type="checkbox" name="topic[]" value="RECFIT">Recreation and Fitness<br /></li>
                                <li><input type="checkbox" name="topic[]" value="CON">Skilled Trade and Construction<br /></li>
                                <li><input type="checkbox" name="topic[]" value="LIBART">Social Sciences and Liberal Arts<br /></li>
                                <li><input type="checkbox" name="topic[]" value="SOCSRV">Social Services<br /></li>
                                <li><input type="checkbox" name="topic[]" value="TRANS">Transportation<br /></li>
                                <li><input type="checkbox" name="topic[]" value="OTHER">Other<br /></li>
                            </ul>

PHP:我设置了三个变量(主题1,2和3)但我不知道要将它们设置为什么。我认为他们必须遍历复选框并在找到值时设置?如何捕获0到3个已检查的值以便我可以插入它们?

<?php
    include('local-connect.php');

    $fname = mysqli_real_escape_string($dbc, $_POST['firstname']);
    $lname = mysqli_real_escape_string($dbc, $_POST['lastname']);   
    $email = $_POST['email'];   
    $password = $_POST['pword'];    
    $address1 = $_POST['lineone'];  
    $address2 = $_POST['linetwo'];  
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];   
    $phone = $_POST['phone'];   
    $ageGroup = $_POST['agegroup'];

    $topic1 =       
    $topic2 = 
    $topic3 = 

    $bio = $_POST['bio'];
    $profilePic = $_POST['fileToUpload'];   

    $query = "INSERT INTO teachers(fname, lname, email, pword, address1, address2, city, state, zip, phone, ageGroup, topic1, topic2, topic3, bio, profilePic)" . 
             "VALUES('$fname','$lname','$email','$password','$address1','$address2','$city','$state','$zip','$phone','$ageGroup','$topic','$bio','$profilePic')";

    $result = mysqli_query($dbc, $query) or die('Unable to Connect to Database or the Registration is incomplete!');

    mysqli_close($dbc);

?>

3 个答案:

答案 0 :(得分:0)

您可以使用jquery执行此操作。 初始化变量

var n=0;

当用户选中复选框时,将n的值增加1

n++;

并同时检查n的值是否等于3.禁用所有复选框。

答案 1 :(得分:0)

您寻找的数据将在

$_POST['topic'];

你应该能够像这样获得前三名:

$topic1 = $_POST['topic'][0];      
$topic2 = $_POST['topic'][1];
$topic3 = $_POST['topic'][2];

答案 2 :(得分:0)

您无法确定是否已设置所有3个(即使使用客户端javascript验证),因此您需要对输入进行一些验证。

$topics = $_POST['topic'];
if(is_array($topics)) {
  // have an array of values
  $topic1 = (array_key_exists(0, topics))? $topics[0] : 'NULL';
  $topic2 = (array_key_exists(1, topics))? $topics[1] : 'NULL';
  $topic3 = (array_key_exists(2, topics))? $topics[2] : 'NULL';
}
else {
  // have single or none value
  $topic1 = (isset($topics))? $topics : 'NULL';
  $topic2 = 'NULL';
  $topic3 = 'NULL';
}