【问题标题】:PHP $_POST[][] two dimensional array to json_encode adding extra single quotesPHP $_POST[][] 二维数组到 json_encode 添加额外的单引号
【发布时间】:2020-08-07 07:00:52
【问题描述】:

问题是我将表单元素命名为如下(突出显示):

我的表单中有数组的集合。现在我想将这些数组保存在具有 json dataType 的 MySql 数据库表中(我正在使用 MySQL v-8 和 PHPDO)。为此,我使用以下函数将我的 $_POST 数组转换为 json 数组:

  $case = json_encode($_POST['case']);

它工作正常并将记录保存在我的表的列中,但问题是它使用以下方式存储键:

在上面的快照中,您可能会看到每个 key 都被 双引号 括起来,然后是 单引号

我需要用 json 列保存相同的记录,但在双引号之后没有单引号

我做错了什么?我将上述从 $_POST['case'] 数组转换为 json 数组的 PHP 代码如下:

    ......
    $case = json_encode($_POST['case']);
    $ret = json_encode($_POST['ret']);
    $discussion = $_POST['discussion'];
    $next_appointment_date = $_POST['next_appointment_date'];
    $remarks = isset($_POST['remarks']) && !empty($_POST['remarks']) ? trim($_POST['remarks']):'';
    $signature = isset($_POST['signature']) && !empty($_POST['signature']) ? trim($_POST['signature']):'';
    //search the same case details before saving...
    $sql_search = "SELECT * FROM case_information 
                   WHERE case_info->>'$.first_party' = :first_party 
                        AND case_info->>'$.second_party' = :second_party 
                        AND case_info->>'$.case_nature' = :case_nature
                        AND case_info->>'$.received_date' = :received_date";
    $parem_search = array(':first_party'=>$_POST['case']["'first_party'"],
                          ':second_party'=>$_POST['case']["'second_party'"],
                          ':case_nature'=>$_POST['case']["'case_nature'"],
                          ':received_date'=>$_POST['case']["'received_date'"]);
        if($db->dbQuery($sql_search,$parem_search)){
            echo('3');
        }else{
            //insert new record
            $sql_insert = "INSERT INTO case_information (
                case_info,
                instituted_by,
                returned_to,
                discussion,
                remarks,
                next_date_apt,
                person_signature,
                entry_made_on
              )
              VALUES
                (
                :case_info,
                :instituted_by,
                :returned_to,
                :discussion,
                :remarks,
                :next_date_apt,
                :person_signature,
                :entry_made_on  
                )";
            $param = array(
                        ':case_info'=>$case,
                        ':instituted_by'=>$ins,
                        ':returned_to'=>$ret,
                        ':discussion'=>$discussion,
                        ':remarks'=>$remarks,
                        ':next_date_apt'=>$next_appointment_date,
                        ':person_signature'=>$signature,
                        ':entry_made_on'=>date('Y-m-d')
                        );
                //echo($sql_insert);
                if($db->dbQuery($sql_insert,$param)){
                        echo('4');
                }else{
                         .
                         .

【问题讨论】:

  • (请不要声明您的问题绝对不是重复的。读者需要您对错过一个问题的可能性持开放态度,如果有人建议,希望您会认真考虑它)。

标签: php mysql json post


【解决方案1】:

您不必在 HTML 表单中使用单引号。只需 array[fieldname] 就足够了。像这样:

<input type="text" name="case[first_party]" id="first_party" />

【讨论】:

    猜你喜欢
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 2019-07-23
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多