【问题标题】:PHP json_encode and insert data to databasePHP json_encode 并将数据插入数据库
【发布时间】:2011-11-19 08:20:39
【问题描述】:

我使用 codeigniter。如何通过json_encode 将此值(在 html 代码中)插入数据库行?

演示: http://jsfiddle.net/AQPwv/3/

<input name="name_r[0][]" value="hi">
<p>
    <input name="units[1][]" value="how">
    <input type="text" name="units[1][]" value="2" style="width:20px;">
    <input type="text" name="units[1][]" value="256314" style="width:65px;">
    <input name="units[1][]" value="fine">
    <input type="text" name="units[1][]" value="4" style="width:20px;">
    <input type="text" name="units[1][]" value="854621" style="width:65px;">
    <p>
        <input type="text" name="price_change[2][]" value="21466" style="width:75px;">
        <input type="text" name="price_change[2][]" value="54219" style="width:75px;">
        <input type="text" name="price_change[2][]" value="48752" style="width:65px;">
        <input type="text" name="price_change[2][]" value="86541" style="width:75px;">
        <input type="text" name="price_change[2][]" value="47276" style="width:75px;">
        <input type="text" name="price_change[2][]" value="74538" style="width:65px;">
        <p>
            <hr>
<p>
    <input name="name_r[0][]" value="hello">
    <p>
        <input name="units[1][]" value="Library">
        <input type="text" name="units[1][]" value="5" style="width:20px;">
        <input type="text" name="units[1][]" value="95641" style="width:65px;">
        <input name="units[1][]" value="khobee">
        <input type="text" name="units[1][]" value="6" style="width:20px;">
        <input type="text" name="units[1][]" value="84527" style="width:65px;">
        <input name="units[1][]" value="PowerTools ">
        <input type="text" name="units[1][]" value="7" style="width:20px;">
        <input type="text" name="units[1][]" value="75462" style="width:65px;">
        <p>
            <input type="text" name="price_change[2][]" value="8457" style="width:75px;">
            <input type="text" name="price_change[2][]" value="134" style="width:75px;">
            <input type="text" name="price_change[2][]" value="76144" style="width:65px;">
            <input type="text" name="price_change[2][]" value="956414" style="width:75px;">
            <input type="text" name="price_change[2][]" value="7546" style="width:75px;">
            <input type="text" name="price_change[2][]" value="123" style="width:65px;">
            <input type="text" name="price_change[2][]" value="84541" style="width:75px;">
            <input type="text" name="price_change[2][]" value="654" style="width:75px;">
            <input type="text" name="price_change[2][]" value="8165" style="width:65px;">
            <input type="text" name="price_change[2][]" value="2145" style="width:75px;">
            <input type="text" name="price_change[2][]" value="354" style="width:75px;">
            <input type="text" name="price_change[2][]" value="4774" style="width:65px;">

更新: 我想将数据存储在数据库表中:

[{
    "name_r": "hi",
    "units": ["how", "2", "256314"],["fine", "4", "854621"],
    "price_change": ["21466", "54219", "48752"],["86541", "47276", "74538"],
}, {
    "name_r": "hello",
    "units": ["Library", "5", "95641"],["khobee", "6", "84527"],["PowerTools ", "7", "75462"],
    "checkbox_units": ["8457", "134", "76144"],["956414", "7546", "123"],["84541", "654", "8165"],["2145", "354", "4774"]
}]

怎么样?

【问题讨论】:

  • 不确定我是否遵循您的问题。为什么要使用 json_encode?您想在数据库的一个字段中插入所有信息吗?还是您希望每条信息位于不同的字段中?
  • 如果你看到 html 代码找到答案这个问题(你为什么要使用 json_encode?)。我有 2 个具有不同值的 html 模拟。我想在您数据库的一个字段中插入所有信息。您有什么建议?
  • 我建议你不要在一个字段中插入所有信息。
  • 如果你想使用支持json类型的数据库,你可以使用MongoDB(mongodb.org/display/DOCS/Introduction
  • @Alfwed 所有信息都是相关的。目前还不清楚用户注册了几个html。它是动态的。不过,你有什么意见?

标签: php arrays codeigniter json


【解决方案1】:

您必须为此更改标记,请按照以下代码进行操作

$name_r=$this->input->post('name_r');
$units=$this->input->post('units');
$price_change=$this->input->post('price_change');

foreach($name_r as $key => $value)
{
    $arr[]=array(
     'name_r'=>$value['0'],
     'units'=>$units[$key],
     'price_change'=>$price_change[$key]
    );
}

$json=json_encode($arr);

// insert $json into your db table

将标记更改为此

<input name="name_r[1][]" value="hi">
<p>
    <input name="units[1][]" value="how">
    <input type="text" name="units[1][]" value="2" style="width:20px;">
    <input type="text" name="units[1][]" value="256314" style="width:65px;">
    <input name="units[1][]" value="fine">
    <input type="text" name="units[1][]" value="4" style="width:20px;">
    <input type="text" name="units[1][]" value="854621" style="width:65px;">
    <p>
        <input type="text" name="price_change[1][]" value="21466" style="width:75px;">
        <input type="text" name="price_change[1][]" value="54219" style="width:75px;">
        <input type="text" name="price_change[1][]" value="48752" style="width:65px;">
        <input type="text" name="price_change[1][]" value="86541" style="width:75px;">
        <input type="text" name="price_change[1][]" value="47276" style="width:75px;">
        <input type="text" name="price_change[1][]" value="74538" style="width:65px;">
        <p>
            <hr>
<p>
    <input name="name_r[2][]" value="hello">
    <p>
        <input name="units[2][]" value="Library">
        <input type="text" name="units[2][]" value="5" style="width:20px;">
        <input type="text" name="units[2][]" value="95641" style="width:65px;">
        <input name="units[2][]" value="khobee">
        <input type="text" name="units[2][]" value="6" style="width:20px;">
        <input type="text" name="units[2][]" value="84527" style="width:65px;">
        <input name="units[2][]" value="PowerTools ">
        <input type="text" name="units[2][]" value="7" style="width:20px;">
        <input type="text" name="units[2][]" value="75462" style="width:65px;">
        <p>
            <input type="text" name="price_change[2][]" value="8457" style="width:75px;">
            <input type="text" name="price_change[2][]" value="134" style="width:75px;">
            <input type="text" name="price_change[2][]" value="76144" style="width:65px;">
            <input type="text" name="price_change[2][]" value="956414" style="width:75px;">
            <input type="text" name="price_change[2][]" value="7546" style="width:75px;">
            <input type="text" name="price_change[2][]" value="123" style="width:65px;">
            <input type="text" name="price_change[2][]" value="84541" style="width:75px;">
            <input type="text" name="price_change[2][]" value="654" style="width:75px;">
            <input type="text" name="price_change[2][]" value="8165" style="width:65px;">
            <input type="text" name="price_change[2][]" value="2145" style="width:75px;">
            <input type="text" name="price_change[2][]" value="354" style="width:75px;">
            <input type="text" name="price_change[2][]" value="4774" style="width:65px;">

下面是 JSON 字符串

[
{"name_r":"hi","units":["how","2","256314","fine","4","854621"],"price_change":["21466","54219","48752","86541","47276","74538"]},

{"name_r":"hello","units":["Library","5","95641","khobee","6","84527","PowerTools ","7","75462"],"price_change":["8457","134","76144","956414","7546","123","84541","654","8165","2145","354","4774"]}
]

【讨论】:

  • &lt;input name="name_r[0][]" value="hi"&gt; 怎么样?没有使用foreach
  • 请回答我的最后一个问题。我很惭愧你对我的问题感到恼火。
  • units 我想要这个:"units": ["how", "2", "256314"],["fine", "4", "854621"],price_change 这个:"price_change": ["21466", "54219", "48752"],["86541", "47276", "74538"], ... 没有"units":["how","2","256314","fine","4","854621"],"price_change":["21466","54219","48752","86541","47276","74538"]},
  • 啊,请帮帮我,我需要你的帮助。
  • 确实应该使用其他功能(对于这种格式)而不是json_encode?
猜你喜欢
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多