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)

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

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题