我想从用户提供数据并将其插入MySql
数据库,,,这是我的代码,,,我的问题是什么?
我的应用程序运行....但在我输入我的数据并单击插入按钮后,没有任何内容插入我的数据库....请帮助我
的init.php
<?php
$db_name="project";
$mysql_user="root";
$server_name="localhost";
$connection=mysqli_connect($server_name,$mysql_user,"",$db_name);
if(!$connection)
{
echo("Connection not successful");
}
else
echo("Connection Successful");
?>
register.php
<?php
require "init.php";
$u_name=$_POST["name"];
$u_password=$_POST["password"];
$u_contact=$_POST["contact"];
$u_country=$_POST["country"];
$sql_query="insert into users (name,password,contact,country) values('$u_name','$u_password','$u_contact','$u_country');";
if(mysqli_query($connection,$sql_query))
{
// echo("data inserted");
}
else
{
// echo("error");
}
?>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goToRegister(View view)
{
Intent intent=new Intent(this,Register.class);
startActivity(intent);
}
}
BackgroundTask.java
public class BackgroundTask extends AsyncTask<String,Void,String> {
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx=ctx;
}
protected String doInBackground(String... params) {
String reg_url="http://127.0.0.1/prj/register.php";
String method=params[0];
if(method.equals("register"))
{
String name=params[1];
String password=params[2];
String contact=params[3];
String country=params[4];
try {
URL url=new URL(reg_url);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream os=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data= URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"+
URLEncoder.encode("contact","UTF-8")+"="+URLEncoder.encode(contact,"UTF-8")+"&"+
URLEncoder.encode("country","UTF-8")+"="+URLEncoder.encode(country,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
os.close();
InputStream IS=httpURLConnection.getInputStream();
IS.close();
return "Registration Successful";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Register.java
public class Register extends AppCompatActivity {
EditText eName,ePassword,eContact,eCountry;
String name,password,contact,country;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Declare Edit Text and Casting
eName= (EditText) findViewById(R.id.EDName);
ePassword= (EditText) findViewById(R.id.EDPassword);
eContact= (EditText) findViewById(R.id.EDContact);
eCountry= (EditText) findViewById(R.id.EDCountry);
}
public void regUser(View view)
{
name=eName.getText().toString();
password=ePassword.getText().toString();
contact=eContact.getText().toString();
country=eCountry.getText().toString();
String method="register";
BackgroundTask backgroundTask=new BackgroundTask(this);
backgroundTask.execute(method,name,password,contact,country);
finish();
}
}
答案 0 :(得分:1)
将您的doInBackground
方法代码更改为:
protected String doInBackground(String... params) {
String reg_url="http://127.0.0.1/prj/register.php";
String method=params[0];
String text = "";
if(method.equals("register"))
{
String name=params[1];
String password=params[2];
String contact=params[3];
String country=params[4];
try {
URL url=new URL(reg_url);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream os=httpURLConnection.getOutputStream();
String data= URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"+
URLEncoder.encode("contact","UTF-8")+"="+URLEncoder.encode(contact,"UTF-8")+"&"+
URLEncoder.encode("country","UTF-8")+"="+URLEncoder.encode(country,"UTF-8");
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
bufferedWriter.write(data);
bufferedWriter.flush();
int statusCode = httpURLConnection.getResponseCode();
if (statusCode == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
sb.append(line).append("\n");
text = sb.toString();
bufferedWriter.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return text;
}
并在你的register.php中:
<?php
require "init.php";
$u_name=$_POST["name"];
$u_password=$_POST["password"];
$u_contact=$_POST["contact"];
$u_country=$_POST["country"];
$sql_query="insert into users (name,password,contact,country) values('$u_name','$u_password','$u_contact','$u_country');";
if(mysqli_query($connection,$sql_query))
{
echo("data inserted");
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error . "<br> Error Code = ".$conn->errno;
}
?>
答案 1 :(得分:0)
如果您使用的是仿真器,请使用10.0.2.2而不是127.0.0.1。如果使用实际的Android设备,那么使用PHP所在的服务器/机器的IP。当通过wifi使用实际设备时,服务器/您的机器应该在同一网络上。