我试图通过我写的一组PHP Web服务,用VBA编写的Access数据库与客户网站上的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数据库中删除该记录。
有没有人有解决方案?
提前致谢。
更新 当我回显$ json时,我得到了JSON编码的字符串,这意味着传递了参数ARE。但是,当我回复$ data [' ExpenseID']时,我得到一个空白的msgbox。
答案 0 :(得分:0)
您的json编码对象是一个包含单个对象的数组,因此如果您想获取该对象的属性,首先必须从数组中对其进行索引。像
这样的东西$txtExpenseID = $data[0]['ExpenseID'];
(同样,我不是一个PHP人,所以只是猜测语法)