gpt4 book ai didi

php - Android PHP连接读取查询时出错

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

我正在尝试保存从 Android 应用程序获取的值并将它们放入本地服务器上的数据库中,但在将数据添加到数据库时我仍然收到空的 toast。我已经检查过,似乎我的脚本在读取查询时停止工作(参见 php 脚本中的注释)。我真的不知道如何强制它插入数据提前致谢

Android 代码:

public class CreateConnectionClass {
String sendPostRequest(String requestURL,HashMap<String,String> postDataParams){
URL url;
String response = "";
try{
url = new URL(requestURL);

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(150000);
httpURLConnection.setConnectTimeout(150000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);

OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(outputStream,"UTF-8"));
bufferedWriter.write(getPostDataString(postDataParams));

bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
int responseCode = httpURLConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
response = bufferedReader.readLine();
}
else{
response = "Error Registering";
}
}
catch(Exception e){
e.printStackTrace();
}
return response;
}

private String getPostDataString(HashMap<String,String> params) throws UnsupportedEncodingException{
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String,String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");

result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}

}

PHP 代码:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo 'inside'; // this one appears in android app toast
require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('No connection');
$user_login = $_POST["user_login"];
$user_password = $_POST["user_password"];
$user_phonenumber = $_POST["user_phonenumber"];
$user_email = $_POST["user_email"];
echo' '.$user_login.','.$user_password.','.$user_phonenumber.','.$user_email.'';
if(!empty($user_login) && !empty($user_password) && !empty($user_phonenumber) && !empty($user_email)){
$query = "SELECT * FROM user_db WHERE user_login = '$user_login' OR user_email = 'user_email' OR user_phonenumber = '$user_phonenumber' ";
$check = mysqli_fetch_array(mysqli_query($dbc,$sql));
echo 'inside'; // here i recicve only </br> in toast and no data and everything stops
if(isset($check)){
echo 'username , email or phone number already exist';

}
else{
$query = "INSERT INTO user_db (user_login,user_password,user_phonenumber,user_email)".
"VALUES('$user_login','$user_password','$user_phonenumber','$user_email')";
if(mysql_query($dbc,$query)){
echo 'sucessfully registered';
}
else{
echo 'try again';
}
}
mysqli_close($dbc);
}
else{
echo 'error';
}
echo 'err,'
}
?>

我正在使用 vamp 服务器并使用

从应用程序连接到脚本
 private static final String REGISTER_URL = "http://192.168.1.5/webapp/register.php";

最佳答案

好的,它正在工作。 php 脚本存在一些错误。更正了 php:

<?php

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

require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('Brak połączenia z serewrem MySQL');
$user_login = $_POST["user_login"];
$user_password = $_POST["user_password"];
$user_phonenumber = $_POST["user_phonenumber"];
$user_email = $_POST["user_email"];

if(!empty($user_login) && !empty($user_password) && !empty($user_phonenumber) && !empty($user_email)){
$query = "SELECT * FROM user_db WHERE user_login = '$user_login' OR user_email = 'user_email' OR user_phonenumber = '$user_phonenumber' ";
$check = mysqli_fetch_array(mysqli_query($dbc,$query));
//echo 'inside';
if(isset($check)){
echo 'username , email or phone number already exist';
}
else{
$query = "INSERT INTO user_db (join_date,user_login,user_password,user_phonenumber,user_email)".
"VALUES(NOW(),'$user_login','$user_password','$user_phonenumber','$user_email')";
mysqli_query($dbc,$query)
or die('Error with database');
echo 'sucessfully registered';
}
}
mysqli_close($dbc);
}
else{
echo 'error';
}
?>

同时更改 android 文件中的一行

if(responseCode == HttpsURLConnection.HTTP_OK){ // Here HttpsURL... instead of HttpURL
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
response = bufferedReader.readLine();
}

关于php - Android PHP连接读取查询时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35783803/

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