通过动态添加的文本框将数据存储到MySQL表中

时间:2017-11-22 16:20:50

标签: php jquery mysql

我目前正在为我正在创建的网站制作一个指南系统,该系统允许注册用户使用"步骤"创建指南。每个"步骤"由简单的JQuery生成,文本框由summernote WYSIWYG编辑器替换。

我正在寻找如何最好地存储每个"步骤"在我的数据库表的一列中。

数据库表

id | category | title | description | guide | author | stamp

指南列将添加每个文本框中的内容。

表格部分

<div class="form-group">
  <p class="text-center">
    <label for="article">Article Content:</label>
    <div class="field_wrapper">
      <div>
        <textarea name="field_name[]" class="form-control summer"></textarea>
        <a href="javascript:void(0);" class="add_button" title="Add field">+ Add Step</a>
      </div>
    </div>
    <script>
      $(document).ready(function() {
        var maxField = 20;
        var x = 1;
        var addButton = $('.add_button');
        var wrapper = $('.field_wrapper');
        var fieldHTML = '<div><textarea name="field_name[]" class="form-control summer"></textarea><a href="javascript:void(0);" class="remove_button" title="Remove field">- Remove Step</a></div>';
        $(addButton).click(function(){
          $(document).ready(function() {
            $('.summer').summernote({
              placeholder: 'Step ' + x
            });
          });
          if(x < maxField){
            x++;
            $(wrapper).append(fieldHTML);
          }
        });
        $(wrapper).on('click', '.remove_button', function(e){
          e.preventDefault();
          $(this).parent('div').remove();
          x--;
        });
      });
      $(document).ready(function() {
        $('.summer').summernote({
          placeholder: 'Step 1'
        });
      });
    </script>
  </p>
</div>

我知道我可以通过以下方式获取所有提交的数据:

$fieldvals = $_REQUEST['field_name'];
foreach($fieldvals as $value){
    // DO STUFF HERE
}

我会用什么来实际将每一步放入&#34;指南&#34;?

我会做类似的事情:

$input[] = $value
$data = implode(',', $input)
INSERT INTO .....

我不能使用逗号进行内爆,但是正确的,好像其中一个步骤包含一个逗号,它会搞砸了吗?

更新

我从下面的建议中收到了以下JSON输出。不知道如何将其纳入&#34;指南&#34;列?

["TEST STEP 1<\/p>",
"TEST STEP 2<\/p>",
"TEST STEP 3<\/p>"]

1 个答案:

答案 0 :(得分:2)

埃洛队友。

我认为你应该使用 json_encode($ array) json_decode($ json,$ true_or_false)而不是 implode($ glue,$ arr)爆炸($ delimiter,$ str),逗号不会弄乱这些东西,以及unicode字符,数组或布尔值。

//Example
$values = 
  [
    'something' => 'something, something, something else', 
    'something_else' => 'something else',
    'double_quotes' => 'something with double quotes "¿que pasa wey?"',
    'chingadera' => "something with ' as apostrophe",
    'coisa' => 'acentuação em ação na missão',
    'taiwan' => '台灣',
    'capital' => 
      [
          'brazil' =>
              [
                  'pt_BR' => 'Brasília',
                  'zh_TW' => 'You dont know, neither do I'
              ],
          'taiwan' => 
              [
                  'zh_TW' => '台北',
                  'en_GB' => 'Taipei',
                  'pt_BR' => 'Taipé'
              ]
      ],
    'bool' => true
  ];
$json = json_encode($values);
echo $json;

现在你得到了:

/*I've pretty printed the JSON for better readability, it will be an one line string*/
{
  "something": "something, something, something else",
  "something_else": "something else",
  "double_quotes": "something with double quotes \"\u00bfque pasa wey?\"",
  "chingadera": "something with ' as apostrophe",
  "coisa": "acentua\u00e7\u00e3o em a\u00e7\u00e3o na miss\u00e3o",
  "taiwan": "\u53f0\u7063",
  "capital": {
    "brazil": {
      "pt_BR": "Bras\u00edlia",
      "zh_TW": "You dont know, neither do I"
    },
    "taiwan": {
      "zh_TW": "\u53f0\u5317",
      "en_GB": "Taipei",
      "pt_BR": "Taip\u00e9"
    }
  },
  "bool": true
}

您可以将此字符串插入数据库列。 现在是解码的时候了

/*JSON to array
Pass true as the second arg on json_decode($json, $bool)*/
$array = json_decode($json, true); //you must pass true as the second arg
echo $array['chingadera']; //'something with \' as apostrophe'
echo $array['taiwan']; //'台灣'
echo $array['capital']['brazil']['pt_BR']; //'Brasília'

/*JSON to STDClass Object
Don't pass anything as the second arg*/
$object = json_decode($json);
echo $object->double_quotes; //'something with double quotes "¿que pasa wey?"'
echo $object->capital->taiwan->zh_TW; //'台北'
echo $object->bool; //1

您可以打印JSONS here

确保您的“指南”列支持大量字符