【问题标题】:Json_decode returning empty value - json object to php arrayJson_decode 返回空值 - json 对象到 php 数组
【发布时间】:2014-04-02 19:14:15
【问题描述】:

我有一个基本表单,我在其中以 json 格式存储(在客户端)文本区域内的值。然后我计划将这些值存储在 mysql 数据库中。我正在使用json_decode 将 json 对象放入常规的 php 数组中。但是当我检查print_r($personArray) 时,什么都没有。因此,每次我提交表单时,都没有存储在 mysql 数据库中,因为 php 数组是空的。如何将值存储在 mysql 数据库的文本区域中?这是LIVE DEMO

if(isset($_POST['submit'])) {
$data = $_POST['data'];
echo $data;
$personArray = json_decode($data, true);
print_r($personArray);


      foreach($personArray as $key => $value){

          $main_role1 = ($value['main1'] == "true") ? 1 : 0;
          $main_role2 = ($value['main2'] == "true") ? 1 : 0;
          $person_fname = $value['firstName'];
          $person_lname = $value['lastName'];
          $person_phone = $value['phone'];

          $query_init2 = "INSERT INTO person (main_role1, main_role2, first_name, last_name, person_phone) VALUES (:main_role1, :main_role2,:person_fname,:person_lname, :person_phone);";
          $query_prep2 = $db_con->prepare($query_init2);
          $insert_result2 = $query_prep2->execute(array(
            "main_role1" => $main_role1,
            "nmain_role2" => $main_role2,
            "person_fname" => $person_fname,
            "person_lname" => $person_lname,
            "person_phone" => $person_phone
          ));

        }
      }

HTML

<textarea name="data" rows='5' cols='60'>

JSON 对象

    [
    {
        "firstName": "Danny",
        "lastName": "LaRusso",
        "ciscoID": "123",
        "academyID": "1",
        "email": "test1@email.com",
        "phone": "(555) 121-2121",
        "fax": "(123) 123-4567",
        "contact_role": true,
        "netacadContact": true,
        "netacadStaff": false,
        "netacadSuccess": false,
        "instructor_role": false
    },
    {
        "firstName": "Sensei",
        "lastName": "Miyagi",
        "ciscoID": "456",
        "academyID": "1",
        "email": "test2@email.com",
        "phone": "(555) 444-2222",
        "fax": "(123) 123-4567",
        "contact_role": false,
        "netacadContact": false,
        "netacadStaff": false,
        "netacadSuccess": false,
        "instructor_role": true
    }
]

【问题讨论】:

    标签: php arrays json type-conversion


    【解决方案1】:

    json_decode 工作正常。问题是当您将值绑定到 SQL 语句时。

    $insert_result2 = $query_prep2->execute(array(
        ":main_role1" => $main_role1,
        ":nmain_role2" => $main_role2,
        ":person_fname" => $person_fname,
        ":person_lname" => $person_lname,
        ":person_phone" => $person_phone
    ));
    

    您应该在数组的键中包含“:”。

    此外,在查询结束时,您不需要另一个 ;.

    $query_init2 = "INSERT INTO person (main_role1, main_role2, first_name, last_name, person_phone) VALUES (:main_role1, :main_role2, :person_fname, :person_lname, :person_phone)";
    

    【讨论】:

    • : 不会改变任何东西。当我调用print_r($personArray); 时,json_object 没有返回任何内容
    • 是的,正确的。但是当我直接从 textarea $data = $_POST['data'] 中获取值并执行 json_decode($data, true) 时,我不起作用
    • 嗯,$_POST['submit'] 是什么?您的 DEMO 的 html 中没有 name="submit"。
    • 哎呀必须给按钮name='submit'。现在还有另一个错误,但显示此foreach($personArray as $key =&gt; $value){
    • 在提交时检查Page 是否有确切的错误。这不是查询。如果我这样做$data="[{"firstName":"Jenny","lastName":"LaRusso","phone":"(555) 121-2121","alt_phone":"(555)..";,看起来效果很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多