我试图将数据存储在本地主机中存在的数据库中,我的 android 和 php 代码似乎工作正常,但它不会将数据写入数据库。
这是我的 android doInBackground
@Override
protected String doInBackground(String... params) {
String type= params[0];
String name;
String user_name;
String user_phone;
String user_address;
String password;
String login_url = "http://192.168.1.5/Sandbox/signup.php";
String register_url = "http://192.168.1.5/Sandbox/Register.php";
if (type.equals("login")){
try {
Log.d("inside", "doInBackground: ");
password = params[2];
name= params[1];
URL url = new URL(login_url);
Log.d("middle", "doInBackground: ");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setDoInput(true);
Log.d("middle", "doInBackground: ");
OutputStream outputstream = http.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputstream, "UTF-8"));
String post_data = URLEncoder.encode("name"+"UTF-8")+"="+URLEncoder.encode(name+"UTF-8")+"&"+URLEncoder.encode("password"+"UTF-8")+"="+URLEncoder.encode(password+"UTF-8");
Log.d("middle", "doInBackground: ");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputstream.close();
InputStream inputstream = http.getInputStream();
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream,"iso-8859-1"));
result="";
String line="";
while ((line=bufferedreader.readLine())!=null){
result+=line;
}
bufferedreader.close();
inputstream.close();
http.disconnect();
inputstream.close();
Log.d("leaving","doInBackground: ");
return "Registration Successful";
} catch (Exception e) {
e.printStackTrace();
}
}
这是我的 php 文件
<html lang="en">
<title>LOGIN</title>
<body>
<?php
require "Conn.php";
$name= $_POST["name"];
$password = $_POST["password"];
$mysql_qry="insert into user_table values('$name','$password')";
if (mysqli_query ($con,$mysql_qry)){
echo "Insert Success";
}
else{
echo "ERROR!".mysqli_error($con);
}
$con->close();
?>
</body>
</html>
toast 消息显示类似未定义索引的内容
数据库更新为没有文本的空白框。
最佳答案
尝试删除 html
标记并仅保留 php :
<?php
require "Conn.php";
$name= $_POST["name"];
$password = $_POST["password"];
$mysql_qry="insert into user_table values('$name','$password')"
if (mysqli_query ($con,$mysql_qry)){
echo "Insert Success";
}
else{
echo "ERROR!".mysqli_error($con);
}
$con->close();
?>
关于php - 无法使用 php 将数据库从 android 插入本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45128606/