doujiabing1228 2011-10-30 09:56
浏览 75
已采纳

在MYSQL中插入JSON

How can insert values on input in database mysql by json_encode and foreach, this html code and php code don't work true and done insert value incomplete in database table as: "[" Or "1"

DEMO: http://codepad.viper-7.com/LAOJXC

I want they as in database table row:

           Column data_1           | Column static
Row1: ["1111111111", "2222222222"] | 12
Row2: ["3333333333", "4444444444"] | 34
Row1: ["5555555555", "6666666666"] | 56

<input name="data_1[]" value="1111111111">
<input name="data_1[]" value="2222222222">
<input name="static[]" value="12">

<input name="data_1[]" value="3333333333">
<input name="data_1[]" value="4444444444">
<input name="static[]" value="34">

<input name="data_1[]" value="5555555555">
<input name="data_1[]" value="6666666666">
<input name="static[]" value="56">

$data = array();
$data_1 = json_encode($_POST[data_1]);
$static = $_POST[static];
foreach($static as $idx=>$val){
    $data[] = array(
              'data_1' => $data_1[$idx],
              'static' => $static[$idx]
                   )
}

$this->db->insert_batch('MyTable', $data);
  • 写回答

1条回答 默认 最新

  • dsuikgi9199 2011-10-30 10:32
    关注

    The reason why your code only inserts single characters to the database is because you convert the array $_POST['data_1'] to a string using json_encode() and then try to access this string as an array later.

    When you access a string with the square brackets notation (in your code: $data_1[$idx]), PHP interprets this as a character-access in the string, resulting in only a single character.

    From the PHP manual:

    Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.

    Look at this working example:

    <form method="post">
    <input name="data_1[]" value="1111111111">
    <input name="data_1[]" value="2222222222">
    <input name="static[]" value="12">
    
    <input name="data_1[]" value="3333333333">
    <input name="data_1[]" value="4444444444">
    <input name="static[]" value="34">
    
    <input name="data_1[]" value="5555555555">
    <input name="data_1[]" value="6666666666">
    <input name="static[]" value="56">
    <input type="submit">
    </form>
    
    <?php
    $data = array();
    $data_1 = $_POST['data_1'];
    $static = $_POST["static"];
    foreach($static as $idx=>$val){
        $data[] = array(
                  'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx*2+1])),
                  'static' => $static[$idx]
                       );
    }
    echo "<pre>";
    print_r($data);
    ?>
    

    DEMO http://codepad.viper-7.com/ca51ZB

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记