我是Android,MySQL和PHP的新手。我在注册页面中遇到了项目问题。当我输入名称和ID值并点击保存按钮时,我的数据在mysql数据库中,但是当我再次填写相同的名称和密码并点击保存时,它表示数据保存到服务器,而不是错误 - > if(res.equals(“1”))error.setText(“Error”)。有人有任何线索吗?感谢。
这是我的代码:
register.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout
android:layout_width="311dp"
android:layout_height="fill_parent"
android:layout_marginLeft="25dp"
android:layout_marginTop="25dp" >
<TextView
android:text="Registration"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25dp"
/>
<TextView
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
/>
<TableRow android:baselineAligned="true" android:layout_width="match_parent">
<TextView
android:text="Name :"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<EditText android:id="@+id/edtname"
android:maxWidth="180sp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center_vertical" >
</EditText>
</TableRow>
<TableRow>
<TextView
android:text="ID no :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<EditText android:id="@+id/edtid"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:inputType="textPassword">
</EditText>
</TableRow>
<TableRow>
</TableRow>
<TextView
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
/>
<TableRow
android:gravity="center"
android:layout_width="match_parent">
<Button android:text="SAVE"
android:id="@+id/btnsave"
android:layout_width="wrap_content"
android:onClick="next"
android:layout_height="wrap_content">
</Button>
<Button android:text="BACK"
android:id="@+id/btnback"
android:onClick="back"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</TableRow>
<TextView
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
/>
<TextView
android:text=""
android:id="@+id/error"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:textSize="20dp"
/>
</TableLayout>
</ScrollView>
Register.java
package com.example.m_vote;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Register extends Activity {
EditText nm,id;
TextView error;
Button save,back;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
nm=(EditText)findViewById(R.id.edtname);
id=(EditText)findViewById(R.id.edtid);
save=(Button)findViewById(R.id.btnsave);
back=(Button)findViewById(R.id.btnback);
error=(TextView)findViewById(R.id.error);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
//TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("name", nm.getText().toString()));
postParameters.add(new BasicNameValuePair("id", id.getText().toString()));
/* String valid = "1";*/
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/appmysql/data.php", postParameters);
String res = response.toString();
res = res.trim();
res = res.replaceAll("\\s+","");
error.setText(res);
if (res.equals("1")) error.setText("Error");
else
{
error.setText("Registration complete! ");
}
}
catch (Exception e) {
id.setText(e.toString());
}
}
});
}
public void back (View theButton)
{
Intent a = new Intent (this,Mainmenu.class);
startActivity(a);
}
}
CustomHttpClient.java
package com.example.m_vote;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
public class CustomHttpClient {
/** The time it takes for our client to timeout */
public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
/** Single instance of our HttpClient */
private static HttpClient mHttpClient;
/**
* Get our single instance of our HttpClient object.
*
* @return an HttpClient object with connection parameters set
*/
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
/**
* Performs an HTTP Post request to the specified url with the
* specified parameters.
*
* @param url The web address to post the request to
* @param postParameters The parameters to send via the request
* @return The result of the request
* @throws Exception
*/
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Performs an HTTP GET request to the specified url.
*
* @param url The web address to post the request to
* @return The result of the request
* @throws Exception
*/
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
data.php
<?php
$nm=$_POST['name'];
$id=$_POST['id'];
$conn = mysql_connect("localhost","root","");
mysql_select_db("projek1");
$query = "INSERT INTO user (ID,name) values ('$id','$nm')";
$result = mysql_query($query) or die("REPORTGagal Query Simpan KRS.");
if (mysql_num_rows($result) == 1){
echo 1;
}
else {
// print status message
echo 0;
}
?>