我在excel電子表格中存儲了兩列數據,我希望將這些數據存儲在MySQL數據庫(本地託管的時刻)中。將excel數據存儲在MySQL數據庫中通過使用JSON的PHP
我正在將電子表格轉換爲JSON字符串,並通過XMLHTTP發送給PHP代碼。這裏是我的VBA代碼:
Sub sendjson()
Dim json As String
Dim filed1 As String
Dim i As Integer
Dim j As Integer
Dim data As String
Worksheets("param").Range("C1").Select
data = "{" + Chr(34) + "data" + Chr(34) + ":["
j = 2
Do While Not (IsEmpty(ActiveSheet.Cells(j, 3)))
j = j + 1
Loop
i = 2
Do While Not (IsEmpty(ActiveSheet.Cells(i, 3)))
If i < j - 1 Then
data = data + ActiveSheet.Cells(i, 3) + ","
Else
data = data + ActiveSheet.Cells(i, 3) + "]}"
End If
i = i + 1
Loop
Worksheets("param").Range("D1").Value = data
json = data
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
objHTTP.Open "POST", "http://localhost/test/jsontomysql.php", False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.send ("field1=" & json)
Set objHTTP = Nothing
End Sub
這裏是jsontomysql.php代碼:
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$data = json_decode($json);
foreach ($data as $name => $value) {
foreach ($value as $entry){
$req = $bdd->prepare('INSERT INTO param (tck, value) VALUES(:tck, :value)');
$req->execute(array(
':tck'=>$entry->tck,
':value'=>$entry->value
));
}
}
?>
我的MySQL數據庫具有以下結構:
PARAM {TCK(VARCHAR255),價值(真的)}
當我運行代碼時,什麼都沒有發生。我敢肯定的問題是圍繞:$data = json_decode($json);
爲了讓事情變得簡單,我直接拷貝JSON字符串在我的PHP代碼,如下:
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
//$phpArray = json_decode($_POST['field1']);
$data = '{
"u1":{"tck":"EUSA1 Curncy","value":0,005},
"u2":{"tck":"EUSA2 Curncy","value":0,0049},
"u3":{"tck":"EUSA3 Curncy","value":0,0048},
"u4":{"tck":"EUSA4 Curncy","value":0,0047},
"u5":{"tck":"EUSA5 Curncy","value":0,0046},
"u6":{"tck":"EUSA6 Curncy","value":0,0045},
"u7":{"tck":"EUSA7 Curncy","value":0,0044},
"u8":{"tck":"EUSA8 Curncy","value":0,0043},
"u9":{"tck":"EUSA9 Curncy","value":0,0042}
}';
$phpArray = json_decode($data, true);
foreach ($phpArray as $key => $value) {
foreach ($value as $entry) {
$req = $bdd->prepare('INSERT INTO param (tck, value) VALUES(:tck, :value)');
$req->execute(array(
':tck'=>$entry->tck,
':value'=>$entry->value
));
}
}
?>
看來我在一個錯誤在我的第一個foreach循環線26 ...
Warning: Invalid argument supplied for foreach() in C:\wamp\www\finance\jsontomysql.php on line 26
編輯:
的問題必須是VBA和PHP之間,因爲,當我這樣做時,SQL數據庫是孔U pdated:
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
//$phpArray = json_decode($_POST['field1']);
$data = '{"u1":{"tck":"EUSA1 Curncy","value":0.005},"u2":{"tck":"EUSA2 Curncy","value":0.0049},"u3":{"tck":"EUSA3 Curncy","value":0.0048},"u4":{"tck":"EUSA4 Curncy","value":0.0047},"u5":{"tck":"EUSA5 Curncy","value":0.0046},"u6":{"tck":"EUSA6 Curncy","value":0.0045},"u7":{"tck":"EUSA7 Curncy","value":0.0044},"u8":{"tck":"EUSA8 Curncy","value":0.0043},"u9":{"tck":"EUSA9 Curncy","value":0.0042}}';
var_dump($data);
$phpArray = json_decode($data, true);
var_dump($phpArray);
foreach ($phpArray as $u) {
$req = $bdd->prepare('INSERT INTO param (tck, value) VALUES(:tck, :value)');
$req->execute(array(
':tck'=>$u['tck'],
':value'=>$u['value']
));
}
?>
這是什麼問題? 謝謝
你如何獲得變量$ json的值? – 2013-03-21 16:26:17