douzheng0702 2015-05-05 05:55
浏览 181
已采纳

VBA(Access 2013)HTTP POST不传递参数

I am attempting to get my Access database written in VBA to communicate with a MySQL database on my clients website through a set of PHP web services I wrote. I have managed to get the Access db to retrieve data from the MySQL db but can't get it to post anything. I have narrowed the problem down to the fact that my HTTP request isn't sending the arguments I assign it.

Here are some questions and sites I have looked at already. These were not helpful, because the majority weren't dealing with PHP and were instead looking directly at websites, or were dealing with GET rather than POST.

Http Post not posting data
excel vba http request download data from yahoo finance
VBA HTTP GET request - cookies with colons
Pass Parameters in VBA HTTP Post Request
Perform hidden http-request from vba
How can I send an HTTP POST request to a server from Excel using VBA?
http://www.tushar-mehta.com/publish_train/xl_vba_cases/vba_web_pages_services/
https://stackoverflow.com/questions/29190759/vba-oauth2-authentication-http-get-request
VBA ServerXMLHTTP https request with self signed certificate
How can I send an HTTP POST request to a server from Excel using VBA?
Sending http requests with VBA from Word Sending http requests with VBA from Word

My code for VBA is:

Dim strJSONEncodedJob As String
        strJSONEncodedJob = "[{""ExpenseID"":""" & astrExpenseIDs(intI) & "}]"
        URL = "I removed the URL when posting"
        objHTTP.Open "POST", URL, False
        objHTTP.setRequestHeader "Content-Type", "application/json"
        objHTTP.send (strJSONEncodedJob)
        strResponse = objHTTP.responseText
        MsgBox strResponse

And my PHP code is:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$json = file_get_contents('php://input');
$data = json_decode($json, true);

$stmt = $conn->prepare("DELETE FROM tblExpenses WHERE ExpenseID=?");
$txtExpenseID = $data['ExpenseID']; 
$stmt->bind_param("i", $txtExpenseID);
$stmt->execute();

echo '{"result" : "success"}';


$stmt->close();
$conn->close();

I get the success statement as a msgbox in VBA, as expected, however the record is not deleted from the MySQL db.

Does anyone have a solution?

Thanks in advance.

Update When I echo $json I get the JSON encoded string, which means the arguments ARE being passed. However, when I echo $data['ExpenseID'] I get a blank msgbox.

  • 写回答

1条回答 默认 最新

  • dongyanzhui0524 2015-05-07 02:43
    关注

    Your json-encoded object is an array containing a single object, so if you want to get at that object's properties you'd first have to index it from the array. Something like

    $txtExpenseID = $data[0]['ExpenseID']; 
    

    (again, I'm not a PHP person so just guessing the syntax)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码