2017-08-10 66 views
0

我收到此錯誤
錯誤:數組字符串轉換在路徑上線37Json在解析php時插入錯誤?


<?php 
    include('config.php'); 
    // read json file 
if($_SERVER['REQUEST_METHOD'] == "GET"){ 
    $filename = 'employee.json'; 
    $json_data = file_get_contents($filename); 
    // $json_data=$_POST['QUESTION']; 
    //convert json object to php associative array 
    $data = json_decode($json_data,true); 

if (is_array($data) || is_object($data)) 
{ 
    for($j=0;$j<sizeof($data);$j++){ 
     //$jsonData[$j] = $data[$j]['DATA']; 
     //$jsonAnswers[$j] = $data[$j]['ANSWERS']; 

     $drmobile = $data[$j]['DATA']['DRMOBILE']; 
     $divcode = $data[$j]['DATA']['DIVCODE']; 
     $brdcode = $data[$j]['DATA']['BRDCODE']; 
     $prdcode = $data[$j]['DATA']['PRDCODE']; 
     $createdon = $data[$j]['DATA']['CREATEDON']; 

    // echo $drmobile." -- "; 
     for($i=0;$i<sizeof($data[$j]['ANSWERS']);$i++) 
     { 

      $quecode[$i] = $data[$j]['ANSWERS'][$i]['ADCODE']; 
      $answer[$i] = $data[$j]['ANSWERS'][$i]['ANSWER']; 

      // $quecodes=$quecode[$i]; 
      // $answers=$answer[$i]; 
      echo $quecode[$i]." <--> ".$answer[$i]."<br/>"; 
     } 
     echo "<br/>-----------<br/>"; 
     $sql="INSERT INTO U_ANSWERS(DRMOBILE,ADCODE,DIVCODE,BRDCODE,PRDCODE,ANSWERS,CREATEDON) 
     VALUES ('$drmobile', '$quecode', '$divcode', '$brdcode', '$prdcode', '$answer', '$createdon');"; 
      $qur = mysql_query($sql); 
     if($qur){ 
     $json = array("status" => 1, "msg" => "Done User added!"); 
    }else{ 
     $json = array("status" => 2, "msg" => "Already Submitted"); 
    } 


    } 


} 

}else{ 
    $json = array("status" => 0, "msg" => "Request method not accepted"); 
} 


@mysql_close($conn); 

/* Output header */ 
// header('Content-type: application/json'); 
echo json_encode($json);  
    //close connection 
?> 

樣品的Json

[{ 
    "DATA": { 
     "DRMOBILE": "8985180306", 
     "DIVCODE": 2, 
     "BRDCODE": 2, 
     "PRDCODE": 2, 
     "CREATEDON": "2017-08-10 11:38:22" 
    }, 
    "ANSWERS": [{ 
     "ADCODE": 1, 
     "ANSWER": "Excellent" 
    }, { 
     "ADCODE": 2, 
     "ANSWER": "Yes" 
    }, { 
     "ADCODE": 3, 
     "ANSWER": "No" 
    }, { 
     "ADCODE": 4, 
     "ANSWER": "Yes" 
    }, { 
     "ADCODE": 5, 
     "ANSWER": "Yes" 
    }, { 
     "ADCODE": 6, 
     "ANSWER": "No" 
    }, { 
     "ADCODE": 7, 
     "ANSWER": "2-4" 
    }, { 
     "ADCODE": 8, 
     "ANSWER": "15" 
    }] 
},{ 
    "DATA": { 
     "DRMOBILE": "8985180305", 
     "DIVCODE": 1, 
     "BRDCODE": 1, 
     "PRDCODE": 1, 
     "CREATEDON": "2017-08-10 11:38:22" 
    }, 
    "ANSWERS": [{ 
     "ADCODE": 1, 
     "ANSWER": "Excellent" 
    }, { 
     "ADCODE": 2, 
     "ANSWER": "Yes" 
    }, { 
     "ADCODE": 3, 
     "ANSWER": "No" 
    }, { 
     "ADCODE": 4, 
     "ANSWER": "Yes" 
    }, { 
     "ADCODE": 5, 
     "ANSWER": "Yes" 
    }, { 
     "ADCODE": 6, 
     "ANSWER": "No" 
    }, { 
     "ADCODE": 7, 
     "ANSWER": "2-4" 
    }, { 
     "ADCODE": 8, 
     "ANSWER": "15" 
    }] 
},{ 
    "DATA": { 
     "DRMOBILE": "8985180307", 
     "DIVCODE": 2, 
     "BRDCODE": 2, 
     "PRDCODE": 2, 
     "CREATEDON": "2017-08-10 11:38:22" 
    }, 
    "ANSWERS": [{ 
     "ADCODE": 1, 
     "ANSWER": "Excellent" 
    }, { 
     "ADCODE": 2, 
     "ANSWER": "Yes" 
    }, { 
     "ADCODE": 3, 
     "ANSWER": "No" 
    }, { 
     "ADCODE": 4, 
     "ANSWER": "Yes" 
    }, { 
     "ADCODE": 5, 
     "ANSWER": "Yes" 
    }, { 
     "ADCODE": 6, 
     "ANSWER": "No" 
    }, { 
     "ADCODE": 7, 
     "ANSWER": "2-4" 
    }, { 
     "ADCODE": 8, 
     "ANSWER": "15" 
    }] 
} 
] 

+1

哪一行是37行?你能提供完整的錯誤信息嗎? –

+0

注意:數組到字符串轉換在C:\ xampp \ htdocs \ android \ testing \ krisapp \ test3.php在第37行這是錯誤消息得到 –

回答

-1

我認爲這個錯誤是因爲你傳遞這些陣列

$quecode[$i] = $data[$j]['ANSWERS'][$i]['ADCODE']; 
    $answer[$i] = $data[$j]['ANSWERS'][$i]['ANSWER']; 

這裏:

VALUES ('$drmobile', '$quecode', '$divcode', '$brdcode', '$prdcode', 
'$answer', '$createdon'); 

所以應該通過這樣的:

VALUES ('$drmobile', '$quecode[$i]', '$divcode', '$brdcode', '$prdcode', 
'$answer[$i]', '$createdon'); 
+0

是的,您的建議是正確的,但我們應該將sql語句放在循環中作爲M Idrees說 –

1

在你的for循環,它表明$quecode$answer是數組。

而在你的SQL語句中,你正在使用它們作爲字符串變量。

$sql="INSERT INTO U_ANSWERS(DRMOBILE,ADCODE,DIVCODE,BRDCODE,PRDCODE,ANSWERS,CREATEDON) 
     VALUES ('$drmobile', '$quecode', '$divcode', '$brdcode', '$prdcode', '$answer', '$createdon');"; 

現在可能有兩種可能的方式:

  1. 將這個SQL語句也環路,並從這些陣列插入所有個人 值($quecode$answer

  2. 結合通過使用將這些($quecode$answer)數組的值轉換爲單個字符串(例如逗號 分隔字符串)功能如:implode(",", $quecode);,然後按照您現在使用的單字符串插入。

+0

我已經把循環中的sql語句,但重複相同的錯誤注意:數組到字符串轉換在C:\ xampp \ htdocs \ android \ testing \ krisapp \ test3.php在34行和數據被插入該表,但quecode插入爲0,並作爲陣列@ M Idrees答案 –

+0

你可以更新你所做的更改,所以我們可以看到。你用這個echo語句得到了什麼輸出...... echo $ quecode [$ i]。「<-->」。$ answer [$ i]。「
」; –

+0

明白了謝謝M Idrees –