2013-03-21 106 views
0

我在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'] 
     )); 
} 
?> 

這是什麼問題? 謝謝

+0

你如何獲得變量$ json的值? – 2013-03-21 16:26:17

回答

0

我終於找到:-)。問題在於發送對象和用於我的數字的分隔符。

將「,」轉換爲「。」後。在我的Excel電子表格中,我必須找出發送的vba對象問題。

工作代碼如下

VBA代碼:(將數據轉換成JSON字符串和發送字符串到我的PHP頁面)

Sub sendjson() 
Dim i As Integer 
Dim j As Integer 
Dim data As String 
Worksheets("param").Range("D1").Select 

data = "{" 

j = 2 
Do While Not (IsEmpty(ActiveSheet.Cells(j, 4))) 
    j = j + 1 
Loop 

i = 2 
Do While Not (IsEmpty(ActiveSheet.Cells(i, 4))) 
    If i < j - 1 Then 
    data = data + ActiveSheet.Cells(i, 4) + "," 
    Else 
    data = data + ActiveSheet.Cells(i, 4) + "}" 
    End If 
    i = i + 1 
Loop 
Worksheets("param").Range("E1").Value = data 

'data --> php 
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") 
    serverURL = "http://localhost/finance/jsontomysql.php" 
    objHTTP.Open "POST", serverURL, False 
    objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
    objHTTP.send ("field1=" & data) 
Set objHTTP = Nothing 
End Sub 

PHP代碼& MySQL查詢:

<?php 
try 
{ 
    $bdd = new PDO('mysql:host=localhost;dbname=test', 'root', ''); 
} 
catch(Exception $e) 
{ 
    die('Erreur : '.$e->getMessage()); 
} 
// $file = fopen("test.txt","w"); 
// echo fwrite($file,$_POST['field1']); 
// fclose($file); 
$data = $_POST['field1']; 
$phpArray = json_decode($data, true); 
foreach ($phpArray as $u) { 
     $req = $bdd->prepare('INSERT INTO param (tck, value) VALUES(:tck, :value)'); 
     $req->execute(array(
      ':tck'=>$u['tck'], 
      ':value'=>$u['value'] 
     )); 
} 
?> 

爲了確保字符串已被PHP代碼接收,我將結果寫入.txt文件(=我的代碼中已註釋的部分)。

現在,MySQL數據庫很好地插入數據庫中的值。

謝謝大家的幫助。

0

要調用您發佈field1領域,因此該行很可能必須是這樣的:

$data = json_decode($_POST['field1']); 

你也應該將你準備的語句出循環;你只需要準備一次。

+0

我編輯我的問題 – Henri 2013-03-21 17:12:39

0

你看過json_decode($ json)實際上在輸出什麼嗎?你是否將數據以一種您期望能夠像嘗試嘗試的方式迭代的形式獲取?

只要嘗試做一個print_r($ json);作爲快速驗證來調試輸出。

它會出現json_decode失敗。在我的測試中,這是因爲你的'價值'沒有被引用。

實施例:

<?php 

$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"}}'; 

$phpArray = json_decode($data, true); 

foreach ($phpArray as $key => $value) { 
    print "$key\n"; 
    print "$value[tck]\n"; 
    print "$value[value]\n"; 
    print "\n\n"; 
} 

?> 

輸出:

marks-mac-pro:~ mstanislav$ php data.php 
u1 
EUSA1 Curncy 
0,005 


u2 
EUSA2 Curncy 
0,0049 


u3 
EUSA3 Curncy 
0,0048 


u4 
EUSA4 Curncy 
0,0047 


u5 
EUSA5 Curncy 
0,0046 


u6 
EUSA6 Curncy 
0,0045 
+0

我編輯我的問題 – Henri 2013-03-21 17:13:40

+0

如果我引用我的'值',他們被視爲字符串。我需要使用浮動。但是,我將「,」改爲「。」現在,我的價值觀被公認爲浮動。但是,錯誤是foreach循環...你有想法嗎? – Henri 2013-03-21 17:37:39

+0

我在上面編輯了我的答案。實際上,我直接複製了PHP代碼中的JSON字符串,以查看我的php/mysql代碼是否正常工作。其實它是。這個問題似乎來自VBA的發送部分,但我不知道它...你有想法嗎?謝謝 ! – Henri 2013-03-22 08:03:37

相關問題