0

我的 php 文件的代码如下所示

<?php

    $connect = mysqli_connect("localhost","root","","surveytest");
    $query = '';
    $table_data = '';
    $filename2 = "employee_data.js";
    $data2 = file_get_contents($filename2);
    $array2 = json_decode($data2, true);


     foreach($array2 as $row) //Extract the Array Values by using Foreach Loop
              {
               $query .= "INSERT INTO survey(name, gender, designation) 
               VALUES 
               ('".$row["name"]."', 
               '".$row["gender"]."', 
               '".$row["designation"]."'); ";  // Make Multiple Insert Query 

               $table_data .= '
                <tr>
           <td>'.$row["name"].'</td>
           <td>'.$row["gender"].'</td>
           <td>'.$row["designation"].'</td>
          </tr>
               '; //Data for display on Web page
              }
              if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
        {
         echo '<h3>Imported JSON Data</h3><br />';
         echo '
          <table class="table table-bordered">
            <tr>
             <th width="45%">Name</th>
             <th width="10%">Gender</th>
             <th width="45%">Designation</th>
            </tr>
         ';
         echo $table_data;  
         echo '</table>';
              }  
    ?>

我的 javascript 文件的代码如下所示

var json =
 {  
  "items": [
    {  
     "name": "Rusydi",  
     "gender": "Male",  
     "designation": "System Architect"  
    },  

    {  
     "name": "Hakim",  
     "gender": "Male",  
     "designation": "Conservation worker"  
    }
 ]
 }

嘿!我是 javascript 和 JSON 的初学者。
我尝试将 var json 添加到 mysql 数据库中。
现在我想参考这个 javascriptfile(var json) 但它不起作用。

我的目的是尝试将此变量存储在 mysql 中。
这就是为什么我尝试这样做。

var json = {
    questions: [
        {
            name: "name",
            type: "text",
            title: "Please enter your name:",
            placeHolder: "Jon Snow",
            isRequired: true
        }, {
            name: "birthdate",
            type: "text",
            inputType: "date",
            title: "Your birthdate:",
            isRequired: true
        }, {
            name: "color",
            type: "text",
            inputType: "color",
            title: "Your favorite color:"
        }, {
            name: "email",
            type: "text",
            inputType: "email",
            title: "Your e-mail:",
            placeHolder: "jon.snow@nightwatch.org",
            isRequired: true,
            validators: [
                {
                    type: "email"
                }
            ]
        }
    ]
};

这是完整的代码。 https://surveyjs.io/Examples/Library/?id=questiontype-text&platform=jQuery&theme=default

Survey
    .StylesManager
    .applyTheme("default");

var json = {
    questions: [
        {
            name: "name",
            type: "text",
            title: "Please enter your name:",
            placeHolder: "Jon Snow",
            isRequired: true
        }, {
            name: "birthdate",
            type: "text",
            inputType: "date",
            title: "Your birthdate:",
            isRequired: true
        }, {
            name: "color",
            type: "text",
            inputType: "color",
            title: "Your favorite color:"
        }, {
            name: "email",
            type: "text",
            inputType: "email",
            title: "Your e-mail:",
            placeHolder: "jon.snow@nightwatch.org",
            isRequired: true,
            validators: [
                {
                    type: "email"
                }
            ]
        }
    ]
};

window.survey = new Survey.Model(json);

survey
    .onComplete
    .add(function (result) {
        document
            .querySelector('#surveyResult')
            .innerHTML = "result: " + JSON.stringify(result.data);
    });

$("#surveyElement").Survey({model: survey});

或者我该怎么办?

4

3 回答 3

0

从文件中删除“var json =”并将扩展名更改为“.json”而不是“.js”。

由于您的 javascript 文件不包含有效的 JSON 字符串,因此 php 无法对其进行解码。

员工数据.json

 {  
  "items": [
    {  
     "name": "Rusydi",  
     "gender": "Male",  
     "designation": "System Architect"  
    },  

    {  
     "name": "Hakim",  
     "gender": "Male",  
     "designation": "Conservation worker"  
    }
  ]
 }
于 2018-10-24T03:10:16.607 回答
0

好的,我看到的问题是这样的:

//employee_data.js
var json =
{

然后你导入那个

$filename2 = "employee_data.js";
$data2 = file_get_contents($filename2);
$array2 = json_decode($data2, true);

JSON 不是 JavaScript 代码(严格来说),它是将 JavaScript 对象格式化或编码为字符串的方式。(Java S cript Object Notation _ 所以你的文件应该以而不是变量设置开头。所以你只需要删除那个位。{var json =

如果您检查var_dump($array2);它可能会说NULL,如果您echo json_last_error_msg()在完成后立即检查,json_decode它可能会说诸如此类的东西Syntax error invalid JSON

这可以像这样复制:

 var_dump(json_decode('var json={"foo":"bar"}', true)); 
 echo json_last_error_msg();

输出:

NULL
Syntax error

沙盒

如果您var json =从我过于简单的示例中删除 ,您会得到:

array(1) {
   ["foo"]=> string(3) "bar"
}
No error

干杯!

于 2018-10-24T03:10:24.913 回答
0

首先,隔离紧随其后的 json 数据,var json =并以.};

然后通过将所有键括在双引号中来修复 json 字符串。

最后,将数据转换为数组,以便您可以使用questions子数组执行查询过程。

*注意,我不建议您使用mysqli_multi_query(),因为它不稳定/不安全。我建议您使用准备好的语句来插入数据。我将避免解释这个任务,因为在 StackOverflow 上有很多很多关于如何做到这一点的示例。

代码:(PHP 演示)(正则表达式 1 演示)(正则表达式 2 演示

if (preg_match('~^var json = \K{.*?}(?=;)~ms', $js_file_contents, $match)) {  // cut away extra
    $json = preg_replace('~^\s*\K\w+~m', '"\0"', $match[0]);  // quote-wrap the keys
    var_export(json_decode($json, true));  // convert json string to array and display
}

输出:

array (
  'questions' => 
  array (
    0 => 
    array (
      'name' => 'name',
      'type' => 'text',
      'title' => 'Please enter your name:',
      'placeHolder' => 'Jon Snow',
      'isRequired' => true,
    ),
    1 => 
    array (
      'name' => 'birthdate',
      'type' => 'text',
      'inputType' => 'date',
      'title' => 'Your birthdate:',
      'isRequired' => true,
    ),
    2 => 
    array (
      'name' => 'color',
      'type' => 'text',
      'inputType' => 'color',
      'title' => 'Your favorite color:',
    ),
    3 => 
    array (
      'name' => 'email',
      'type' => 'text',
      'inputType' => 'email',
      'title' => 'Your e-mail:',
      'placeHolder' => 'jon.snow@nightwatch.org',
      'isRequired' => true,
      'validators' => 
      array (
        0 => 
        array (
          'type' => 'email',
        ),
      ),
    ),
  ),
)
于 2018-10-24T09:55:17.763 回答