在一个字段中存储多个关键字

时间:2015-01-07 14:37:19

标签: javascript php mysql

在我的数据库中,我希望能够将多个关键字存储到MySQL数据库的一个字段中,就像从视频存储标记并用逗号分隔每个单词一样。我怎样才能做到这一点?     

//for($i = 1; isset($_POST['brandKeyword' . $i]); $i++){
//    $brandName = $_POST['brandName' . $i];
//    $brandCategory = $brandCategory['brandCategory' . $i];
//    $brandKeyword = $brandKeyword['brandKeyword' . $i];
//}

if (isset($_POST['submit'])) {
    // Process the form
    // validations
    $required_fields = array("brandName", "brandCategory" , "brandKeyword");
    validate_presences($required_fields);

//    for($i = 1; isset($_POST['brandKeyword' . $i]); $i++){
//        $brandName = $_POST['brandName' . $i];
//        $brandCategory = $brandCategory['brandCategory' . $i];
//        $brandKeyword = $brandKeyword['brandKeyword' . $i];
//    }

    if (empty($errors)) {
    // Perform Create

    $brandName = mysql_prep($_POST["brandName"]);
    $brandCategory = mysql_prep($_POST["brandCategory"]);
    $brandKeyword = mysql_prep($_POST["brandKeyword"]);

    $query  = "INSERT INTO brands (";
    $query .= "  brandName, brandCategory, brandKeyword";
    $query .= ") VALUES (";
    $query .= "  '{$brandName}', '{$brandCategory}', '{$brandKeyword}'";
    $query .= ")";
    $result = mysqli_query($connection, $query);

    if ($result) {
    // Success
    $_SESSION["message"] = "Brand has been stored";
    redirect_to("add_brands.php");
    } else {
    // Failure
    $_SESSION["message"] = "Information could not be stored";
    }
    }
    } else {
    // This is probably a GET request
    } // end: if (isset($_POST['submit']))
?>


<form id="manage_brands" action="add_brands.php" method="post">
 <form role="form" method="post">
    <h2>Add Brand Information</h2>
       <p>Brand Name:
         <input type="text" name="brandName" value="" autofocus/>
       </p>
       <p>Brand Category:
         <select type="text" name="brandCategory">
           <option value="">Select...</option>
           <option value="Animation">Animation</option>
           <option value="Automotive">Automotive</option>
           <option value="Beauty and Fashion">Beauty & Fashion</option>
           <option value="Comedy">Comedy</option>
           <option value="Cooking and Health">Cooking & Health</option>
           <option value="DIY">DIY</option>
           <option value="Fashion">Fashion</option>
           <option value="Film and Entertainment">Film & Entertainment</option>
           <option value="Food and Drink">Food & Drink</option>
           <option value="Gaming">Gaming</option>
           <option value="Lifestyle">Lifestyle</option>
           <option value="Music">Music</option>
           <option value="News and Politics">News & Politics</option>
           <option value="Science&Education">Science & Education</option>
           <option value="Sports">Sports</option>
           <option value="Technology">Technology</option>
           <option value="Television">Television</option>
        </select>
        </p>
        <p class="text-box">
          <label for="box1">Brand Keyword:<span class="box-number">1</span></label>
          <input type="text" name="brandKeyword" value="" id="box1" />
          <a class="add-box" href="#"><img id="icon" src="images/plus.png"></a>
        </p>
        <input type="submit" name="submit" value="Store Brand" onclick="return confirm('Do you wish to add a new brand?');"/>
        <br />
        <br />
        <a href="home.php">Cancel</a>
    </form> 

我也可以选择添加或删除更多文本框,如果我输入一个键,一切正常,但如果我输入多个,则只读取我输入的最后一个关键字

jQuery(document).ready(function($){
    $('#manage_brands .add-box').click(function(){
        var n = $('.text-box').length + 1;

        var box_html = $('<p class="text-box"><label for="box' + n + '">Brand Keyword <span        class="box-number">' + n + '</span></label> <input type="text" name="brandKeyword" value="" id="box' + n + '" /> <a href="#" class="remove-box"><img id="icon" src="images/minus.png"></a></p>');
    box_html.hide();
    $('#manage_brands p.text-box:last').after(box_html);
    box_html.fadeIn('slow');
    return false;
    });

$('#manage_brands').on('click', '.remove-box', function(){
    $(this).parent().css( 'background-color', '#ffffff' );
    $(this).parent().fadeOut("slow", function() {
        $(this).remove();
        $('.box-number').each(function(index){
            $(this).text( index + 1 );
        });
    });
    return false;
});
});

1 个答案:

答案 0 :(得分:0)

如前所述,这是糟糕的设计,你应该避免它。但是,如果您确实想要使用它,我会建议使用通用格式,例如JSON而不是您编写的内容(可能存在问题并且容易出错)。 php提供了json编码和解码功能。

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.json-encode.php