我正在尝试让我的 Access 数据库用 VBA 编写,以便通过我编写的一组 PHP Web 服务与我客户网站上的 MySQL 数据库进行通信。我已经设法让 Access 数据库从 MySQL 数据库检索数据,但无法让它发布任何内容。我已将问题范围缩小到以下事实:我的 HTTP 请求未发送我分配给它的参数。
以下是我已经浏览过的一些问题和网站。这些没有帮助,因为大多数人不处理 PHP,而是直接查看网站,或者处理 GET 而不是 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
我的 VBA 代码是:
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
我的 PHP 代码是:
// 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();
正如预期的那样,我在 VBA 中以 msgbox 的形式获得了成功语句,但是该记录并未从 MySQL 数据库中删除。
有人有解决办法吗?
提前致谢。
更新 当我 echo $json 时,我得到了 JSON 编码的字符串,这意味着参数正在被传递。但是,当我 echo $data['ExpenseID'] 时,我得到一个空白的消息框。
最佳答案
您的 json 编码对象是一个包含单个对象的数组,因此如果您想获取该对象的属性,您首先必须从数组中对其进行索引。类似的东西
$txtExpenseID = $data[0]['ExpenseID'];
(再说一次,我不是 PHP 人员,所以只是猜测语法)
关于php - VBA (Access 2013) HTTP POST 不传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30045251/