簡體   English   中英

PHP-將帶有復選框的多個類別添加到mysql中

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

我想做的是在表RECORD中添加一條新記錄。 此新記錄可以與表CATEGORY中的多個類別關聯。 現在,我編寫了以下代碼,以便可以選擇需要與記錄關聯的所有類別。 但是,我不知道如何將所選類別發布到RECORD表中。 因此,例如,如果您在CATEGORY表中有5個類別,分別名為“一個”,“兩個”,“三個”,“四個”和“五個”(ID 1、2、3、4、5),然后選擇“要添加到新記錄中的“ 1”和“ 3”,則應將其記錄在RECORD表中的ID為“ 1、3”的category_id列中。

我在表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);
?>

查看SET數據類型 我相信只要沒有太多的類別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()); 

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM