简体   繁体   English

PHP-将带有复选框的多个类别添加到mysql中

[英]PHP - Add multiple categories with checkbox into mysql

What I want to do is to add a new record in table RECORD. 我想做的是在表RECORD中添加一条新记录。 This new record can be connected with multiple categories from table CATEGORY. 此新记录可以与表CATEGORY中的多个类别关联。 Now, I have made the following code so I can select all categories that need to be connected with the records. 现在,我编写了以下代码,以便可以选择需要与记录关联的所有类别。 However, I don't know how I can post the selected categories into the RECORD table. 但是,我不知道如何将所选类别发布到RECORD表中。 So for example, if you have 5 categories in the CATEGORY table, named 'one', 'two', 'three', 'four' and 'five' (ID 1, 2, 3, 4, 5) and you select 'one' and 'three' to be added to the new record, it should be posted in the RECORD table into the column category_id with their ID '1, 3. 因此,例如,如果您在CATEGORY表中有5个类别,分别名为“一个”,“两个”,“三个”,“四个”和“五个”(ID 1、2、3、4、5),然后选择“要添加到新记录中的“ 1”和“ 3”,则应将其记录在RECORD表中的ID为“ 1、3”的category_id列中。

I already have a column 'category_id' in the table RECORD, but it does not add anything. 我在表RECORD中已经有一列'category_id',但是它没有添加任何内容。 I have the following code. 我有以下代码。 Really hope someone can help me, I would appreciate it big time. 真的希望有人能帮助我,我很乐意为您提供帮助。 I've searched quite a few questions but I still can't solve it :( Many thanks in advance! 我已经搜索了很多问题,但仍然无法解决:(非常感谢!

record.php 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 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);
?>

Look into the SET data type . 查看SET数据类型 I believe it will do what you want, as long as there aren't too many category IDs. 我相信只要没有太多的类别ID,它就能满足您的要求。

You would have to define your category_id column to be SET(1,2,3,4,5...) and then you can insert data as follows: 您必须将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()); 

Hope this helps. 希望这可以帮助。

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

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