Input data from android to database in mysql

2019-08-27 19:21发布

I have this code:

<?php include('konek.php');

$entid=$_POST['entId'];
$entSender=$_POST['entSender'];
$entTitle=$_POST['entTitle'];
$entDate=$_POST['entDate'];
$entSAGrade=$_POST['entSAGrade'];
$entReason=$_POST['entReason'];
$entProblem=$_POST['entProblem'];
$entTime=$_POST['entTime'];

if($row_num != 0)
{
$ins=mysql_query("INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem) VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$‌​entProblem')");
echo "Added"; }
else 
{   echo "Added."; }
?>

My problem is, when i input the data in my android simulator, it is not adding. What is the problem in my code?

And here is my code in android:

public class AddEntry extends Activity {

Button buttonSave;
EditText ent1, ent2, ent3, ent4, ent5, ent6 ;
HttpPost httppost;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addentry);

    buttonSave = (Button)findViewById(R.id.Save);  
    ent1 = (EditText)findViewById(R.id.entSender);
    ent2 = (EditText)findViewById(R.id.entDate);
    ent3 = (EditText)findViewById(R.id.entTitle);
    ent4 = (EditText)findViewById(R.id.entSAGrade);
    ent5 = (EditText)findViewById(R.id.entReason);
    ent6 = (EditText)findViewById(R.id.entProblem);

    buttonSave.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(AddEntry.this, "", 
                    "Adding entry. . .", true);
             new Thread(new Runnable() {
                    public void run() {
                        insertEntry();

                    }

                  }).start();               
        }
    });

}

void insertEntry(){
    try{            

        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("http://10.0.2.2/smcfi/insertentry.php"); // make sure the url is correct.
        //add your data
        nameValuePairs = new ArrayList<NameValuePair>(6);
        // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
        nameValuePairs.add(new BasicNameValuePair("enSender",ent1.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
        nameValuePairs.add(new BasicNameValuePair("entDate",ent2.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entTitle",ent3.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entSAGrade",ent4.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entReason",ent5.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entProblem",ent6.getText().toString().trim()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
        response=httpclient.execute(httppost);
        // edited by James from coderzheaven.. <span id="IL_AD10" class="IL_AD">from here</span>....
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        System.out.println("Response : " + response); 
        runOnUiThread(new Runnable() {
            public void run() {
               // tv.setText("Response from PHP : " + response);
                dialog.dismiss();
            }
        });

        if(response.equalsIgnoreCase("Added")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(AddEntry.this,"Successfully Added.", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(AddEntry.this, Entry.class);
                    AddEntry.this.startActivity(intent);
                    AddEntry.this.finish();
                }
            });

        }else{
            showAlert();                
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}
public void showAlert(){
    AddEntry.this.runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(AddEntry.this);
            builder.setTitle("Error");
            builder.setMessage("Not Added.")  
                   .setCancelable(false)
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                       }
                   });                     
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}
@Override
public void onBackPressed() {
    Intent intent = new Intent(AddEntry.this, MainAct.class);
    AddEntry.this.startActivity(intent);
    AddEntry.this.finish();
}

}

I dont know what is the error. but it is not adding. my inputted data is not adding to my database.

3条回答
The star\"
2楼-- · 2019-08-27 19:23

Replace

$ins=mysql_query("INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem'));"

with

$ins=mysql_query("INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem')");

PS: you're using mysql extension, which is deprecated; moreover, you're opened to sql injection. A better code would be

<?php include('konek.php');
$connect = mysqli_connect("localhost","username","password","yourdb");
$entid=addslashes($_POST['entId']);
$entSender=addslashes($_POST['entSender']);
$entTitle=addslashes($_POST['entTitle']);
$entDate=addslashes($_POST['entDate']);
$entSAGrade=addslashes($_POST['entSAGrade']);
$entReason=addslashes($_POST['entReason']);
$entProblem=addslashes($_POST['entProblem']);
$entTime=addslashes($_POST['entTime']);

if($row_num != 0)
{
$ins=mysqli_query($connect,"INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem')");
echo "Added"; }
else 
{   echo "Added."; }
?>
查看更多
Lonely孤独者°
3楼-- · 2019-08-27 19:29

First you should test your PHP service isolated, e.g. using Chrome's PostMan extension. As you make sure PHP works properly, next step is to connect Android's code to your web service.

Please check PostMan for a nice debugging ;)

https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

查看更多
Melony?
4楼-- · 2019-08-27 19:36

Found a typo -

 $ins=mysql_query("INSERT into tblentry
     (entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
     VALUES
     ('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem'));"
                                                                                    ^=== This should be inside round bracket


Change to this -

$ins=mysql_query("INSERT into tblentry
                 (entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
                 VALUES
                 ('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem')");
查看更多
登录 后发表回答