gpt4 book ai didi

java - mysql insert with java and php : no error but no insert

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:04 25 4
gpt4 key购买 nike

我有一个调用远程 php 将数据插入 mysql 表的 Android java 方法。

这是java代码客户端:

public static void insert(String email1, String email2) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email1", email1));
nameValuePairs.add(new BasicNameValuePair("email2", email2));

try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("myserver:8080/insert.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//How to catch and print php echo() here??
}

这是 insert.php 服务器端(重新编辑):

<?php   
$mysqli_connection = new mysqli("localhost:3306", "root", "password", "mydb");
if ($mysqli_connection->connect_errno) {
echo ("Connection Failure");
exit();
}


$stmt = $mysqli->stmt_init();
if ($stmt->prepare("INSERT INTO `mytable` (`email1`, `email2`) VALUES (?,?)")) {
echo 'Success';
$stmt->bind_param("ss", $_GET['email1'], $_GET['email2']);
$stmt->execute();
$stmt->close();
} else {
echo 'Error Inserting Content';
}

$mysqli->close();
?>

当我调用我的 java insert() 方法时,没有返回异常,但没有完成插入并且 mytable 是空的。

  • 1) 怎么了?
  • 2) 如何查看发生了哪个错误?如何查看 php 端执行的“日志”?

非常感谢。格特里

最佳答案

对列名和/或表名使用反引号而不是单引号

INSERT INTO mytable(`email1`,`email2`) VALUES('$email1','$email2')

什么时候使用反引号?

例如,

CREATE TABLE `hello word`(`Record ID` INT,....)

并且您的查询存在 SQL 注入(inject)漏洞,请花时间阅读下面的文章

关于java - mysql insert with java and php : no error but no insert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13331095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com