1
我试图通过我的java程序来构造一个URL,它会将URL发送到我的php服务器,因此将一行插入我的mysql数据库。我能够手动发送URL(通过我的浏览器地址栏)到我的PHP服务器,并成功地将一行插入到我的表中。HttpURLConnection到PHP但没有MySQL发布?
但是,当我在我的Java程序中构造URL并尝试将请求发送到PHP服务器时,我没有插入任何行。有什么奇怪的是,当我手动将它放入我的地址栏时,java程序构造的url就起作用了。我确实在我的php错误日志中发生错误,但是从我可以告诉一切看起来很好的错误。
任何事情都可以帮助,谢谢。
的Java:
public void updateDB() throws IOException {
final String USER_AGENT = "Mozilla/5.0";
final String POST_URL = "https://example.website/php/php.php?";
final String POST_PARAMS = "charName=" + "\"myname\"" + "&logType=" + "\"Magic_logs\"" + "&magicPrice=" + pricePerMagicLog + "&yewPrice=" + pricePerYewLog + "&totalWealth="+ totalWealth;
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpsURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("POST request not worked");
}
println(POST_URL+POST_PARAMS);
}
PHP:
<?php require_once('muleconnect.php');
$charName = $_GET['charName'];
$logType = $_GET['logType'];
$magicPrice = $_GET['magicPrice'];
$yewPrice = $_GET['yewPrice'];
$totalWealth = $_GET['totalWealth'];
$sql = "INSERT INTO table(char_name, log_type, magic_log_price,
yew_log_price, total_wealth)
VALUES ($charName, $logType, $magicPrice, $yewPrice, $totalWealth)";
$query = $muleconnection->query($sql);
echo 'OK..';
?>
PHP错误日志:
[08-Oct-2017 14:00:47 America/New_York] PHP Notice: Undefined index: charName in /home/dumamwny/public_html/php/php.php on line 4
[08-Oct-2017 14:00:47 America/New_York] PHP Notice: Undefined index: logType in /home/dumamwny/public_html/php/php.php on line 5
[08-Oct-2017 14:00:47 America/New_York] PHP Notice: Undefined index: magicPrice in /home/dumamwny/public_html/php/php.php on line 6
[08-Oct-2017 14:00:47 America/New_York] PHP Notice: Undefined index: yewPrice in /home/dumamwny/public_html/php/php.php on line 7
[08-Oct-2017 14:00:47 America/New_York] PHP Notice: Undefined index: totalWealth in /home/dumamwny/public_html/php/php.php on line 8
成功构建URL字符串(即手工作品,不programtica lly)
https://example.website/php/php.php?charName=%22example_name%22&logType=%22Magic%20logs%22&magicPrice=1174&yewPrice=340&totalWealth=7145753
非常感谢你,我今晚晚些时候会尝试一下,希望它能解决问题。就表名而言,我只是把这个作为一个例子,我的实际表名被编辑出来了。 – RDumais
在我的php文件中更改GET到POST。谢谢! Upvoted和最好的回答。 – RDumais