gpt4 book ai didi

php - 我想知道为什么不向MySQL发送数据?

转载 作者:行者123 更新时间:2023-11-29 17:19:51 24 4
gpt4 key购买 nike

我想将数据发送到数据库。我的 PHP 文件没问题,因为我已经用 postman 测试过它并将数据发送到数据库。但是当我使用 Android 应用程序时,它不应该将数据发送到数据库.这是我发送数据的java方法。请回复我解决方案。

 public void insertData(String name,String category,int quantity){
String url = "http://192.168.172.2/foods/insert.php";
try {
String data = URLEncoder.encode("name", "UTF-8")
+ "=" + URLEncoder.encode(name, "UTF-8");

data += "&" + URLEncoder.encode("category", "UTF-8") + "="
+ URLEncoder.encode(category, "UTF-8");

data += "&" + URLEncoder.encode("quantity", "UTF-8")
+ "=" + URLEncoder.encode(String.valueOf(quantity), "UTF-8");


URL url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(data);
writer.flush();

Log.d("datainsert","success"+data);

} catch (MalformedURLException e) {
Log.d("datainsert","Error"+e.getMessage());

} catch (IOException e) {
Log.d("datainsert","Error"+e.getMessage());
}

<?PHP
require "config.php";

global $connect;

if (isset($_POST['name']) and isset($_POST['category']) and isset($_POST['quantity'])) {
$name = $_POST['name'];
$category = $_POST['category'];
$quantity = $_POST['quantity'];
$intq = (int)$quantity;


if ($_SERVER['REQUEST_METHOD']== "POST") {

$query = "INSERT INTO food_details(food_name,category,quantity) VALUES('$name','$category',$intq);";

if($q = mysqli_query($connect,$query)){
echo "successfully inserted";
}else{
//echo mysqli_error(mysqli_query($connect,$query));
echo "not success";
echo "Error: " . $query . "<br>" . $connect->error;
}
}
}

mysqli_close($connect);
?>

最佳答案

使用POST方法向服务器发送数据Uri.Builder类用于创建查询字符串。如下

Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("firstParam", paramValue1)
.appendQueryParameter("secondParam", paramValue2)
String query = builder.build().getEncodedQuery();

对于您的具体问题,您的发布方法如下

try {
URL url = new URL("http://192.168.172.2/foods/insert.php");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000); //change according to need
conn.setConnectTimeout(15000); //change according to need
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("name", name)
.appendQueryParameter("category", category)
.appendQueryParameter("quantity", quantity);
String query = builder.build().getEncodedQuery();

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();

conn.connect();

//get response from server
String response = conn.getResponseMessage();
Log.d("URl->", response);

} catch (IOException e) {
e.printStackTrace();
}

希望它对你有用

关于php - 我想知道为什么不向MySQL发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51319421/

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