0

我在一个 Excel 电子表格中有两列数据,我想将它们存储在 MySQL 数据库中(目前在本地托管)。

我正在将我的电子表格转换为我通过 XMLHTTP 发送到 PHP 代码的 JSON 字符串。这是我的 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 DB 具有以下结构:

参数{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 DB 得到了很好的更新:

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

问题是什么?谢谢

4

3 回答 3

0

您正在调用您要发布的字段field1,因此该行可能必须类似于:

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

您还应该将您的准备语句移出循环;你只需要准备一次。

于 2013-03-21T16:26:57.530 回答
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
于 2013-03-21T16:28:01.127 回答
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 DB 很好地将值插入到 DB 中。

感谢大家的帮助。

于 2013-03-22T10:05:08.217 回答