PHP - 在mysql中添加带复选框的多个类别

时间:2014-03-05 18:37:31

标签: php mysql sql checkbox

我想要做的是在表RECORD中添加一条新记录。 此新记录可以与表CATEGORY中的多个类别相关联。 现在,我已经制作了以下代码,因此我可以选择需要与记录连接的所有类别。但是,我不知道如何将所选类别发布到RECORD表中。 例如,如果您在CATEGORY表中有5个类别,名为“一个”,“两个”,“三个”,“四个”和“五个”(ID 1,2,3,4,5)并选择'一个'和'三'要添加到新记录中,它应该在RECORD表中发布到列category_id中,其ID为1,3。

我在表RECORD中已经有一个列'category_id',但它没有添加任何内容。我有以下代码。真的希望有人可以帮助我,我会很感激它的大好时光。我搜索了很多问题,但我仍然无法解决:(非常感谢提前!

record.php

<form method="post" action="<?php echo $home; ?>add_form.php">

        <div class="alert alert-info">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <strong><i class="icon-user icon-large"></i>&nbsp;Test</strong>&nbsp;
        </div>

    <table width="400" border="0" cellspacing="1" cellpadding="2">
        <tr>
            <td width="100">Name</td>
            <td><input name="name" type="text" id="name"></td>
        </tr>
    </table>
<br />
    <table width="400" border="0" cellspacing="1" cellpadding="2">
        <tr>

            <table cellpadding="0" cellspacing="0" border="0">
                    <?php 
                    $query=mysql_query("select * from category")or die(mysql_error());
                    while($row=mysql_fetch_array($query)){
                        $id=$row['id'];
                    ?>
                    <tr>
                        <td width="22%"></td>
                        <td width="5%" style="padding-bottom: 4px"><input name="selector[]" type="checkbox" value="<?php echo $id; ?>"></td>
                        <td width="20%" style="padding-top:3px; padding-bottom: 1px"><?php echo $row['type'] ?></td>
                    </tr>
                    <?php  } ?>
            </table>

        </tr>
        <tr>
            <td><br /><br /></td>
        </tr>
    </table>

<input name="save" type="submit" id="save" class="btn btn-success" value="tEST">

</form>

add_form.php

<?php

require_once($_SERVER['DOCUMENT_ROOT'].'/dbconnect.php');

//specify here the Database name we are using
$name = $_POST['name'];
$category_id = isset($_POST['$id[$i]']) ? 1 : 0;

$sql = "INSERT INTO `test`.`record` (`id`, `name`, `category_id`, `date_added`) 
        VALUES (NULL, '{$name}', '{$category_id}', CURRENT_TIMESTAMP());";
//using mysql_query function. it returns a resource on true else False on error
$retval = mysql_query( $sql, $conn );    
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
?>
                    <script type="text/javascript">
                        alert("New Record is Added to the Database");
                        window.location = "record.php";
                    </script>
                    <?php
//close of connection
mysql_close($conn);
?>

1 个答案:

答案 0 :(得分:0)

查看SET data type。我相信它会做你想要的,只要没有太多的类别ID。

您必须将category_id列定义为SET(1,2,3,4,5...),然后才能按以下方式插入数据:

$name = $_POST['name'];
$category_id = implode(',', $_POST['selector']);

INSERT INTO `test`.`record` (`id`, `name`, `category_id`, `date_added`) VALUES (NULL, '{$name}', '{$category_id}', CURRENT_TIMESTAMP()); 

希望这有帮助。