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.

doumiyi7063
doumiyi7063 发表了答案......
5 年多之前 回复
duanchu3376
duanchu3376 谢谢!索引做到了。这个问题一直在扼杀我,你修复了它。非常感谢。如何设置评论作为答案?
5 年多之前 回复
duanlu5055
duanlu5055 你的json编码对象是一个包含单个对象的数组,所以如果你想获得该对象的属性,你首先必须从数组中索引它。类似于$txtExpenseID=$data[0]['ExpenseID'];(再次,我不是一个PHP人,所以只是猜测语法)
5 年多之前 回复
donglou8371
donglou8371 对不起这是一个错字,我会修改它。当我回显$json时,我得到了json字符串,这是我之前没有的,所以这表明参数正在被传递。但是我仍然无法执行该命令(删除MySQL中的记录)。当我回显$data['ExpenseID']时我什么都没得到。
5 年多之前 回复
donglanying3855
donglanying3855 如果你回显$json和$txtExpenseID,你会看到你的期望吗?而且我不是PHP人员,但是在将它作为参数绑定之前,你不会填充$txtExpenseID的值吗?
5 年多之前 回复

1个回答



你的json编码对象是一个包含单个对象的数组,所以如果你想获得该对象的属性,你首先要有 从数组中索引它。 类似</ p>

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

</ code> </ pre>

(同样,我不是一个PHP人员所以只是猜测语法)</ p>
</ div>

展开原文

原文

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)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐