android代码中的错误,无法将数据发送到mysql

时间:2014-04-19 15:46:25

标签: php android

我正在尝试将一些数据发送到php代码,它可以将它存储在table.I已经测试了它正确执行的PHP代码并且数据库中的数据是正确存储的,但是当被android代码调用时会发生异常以下是代码:

// leaveapp.java

package com.example.eleave;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;


import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

//import com.example.datepicker.R;

public class LeaveApp extends Activity {
     private TextView Output1,Output2,tv7,tv8;
     EditText e1;
     String date1,date2;
     String type;

        private Button changeDate1,changeDate2,btn1;
        int flag1=0,flag2=0;
        private int year;
        private int month;
        private int day;
        private Spinner spinner2;
        static final int DATE_PICKER_ID1 = 1111; 
        static final int DATE_PICKER_ID2 = 1110;
        RadioGroup rg2;
            @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.leaveapp);
            Bundle b1=getIntent().getExtras();
          final String username= b1.getString("username");
            Output1 = (TextView) findViewById(R.id.textView7);
            Output2= (TextView) findViewById(R.id.textView8);
            changeDate1 = (Button) findViewById(R.id.button2);
            changeDate2 = (Button) findViewById(R.id.button3);
           spinner2=(Spinner) findViewById(R.id.spinner2);
            tv7=(TextView) findViewById(R.id.textView7);
            tv8=(TextView) findViewById(R.id.textView8);
            btn1=(Button) findViewById(R.id.button1);
            e1=(EditText) findViewById(R.id.editText1);
          addItemsOnSpinner2();
            rg2=(RadioGroup) findViewById(R.id.radiogroup2);
            rg2.setOnCheckedChangeListener(new OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(RadioGroup arg0, int arg1) {
                    // TODO Auto-generated method stub
                    switch(arg1)
                    {
                    case R.id.radioButton5:
                        flag1=1;
                        flag2=0;
                        type="0";
                        break;
                    case R.id.radioButton6:
                        flag2=1;
                        flag1=0;
                        type="1";
                        break;
                    }
                }
                });
          final Calendar c = Calendar.getInstance();
              year=2014;
            month=c.MONTH+1;
            day=c.DAY_OF_MONTH;
                 // Button listener to show date picker dialog
              changeDate1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                            // On button click show datepicker dialog 
                    showDialog(DATE_PICKER_ID1);
               }
             });
            changeDate2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                                // On button click show datepicker dialog 
                    showDialog(DATE_PICKER_ID2);
               }
            });
         btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                if(e1.getText().length()==0 || (flag1==0 && flag2==0)||tv7.getText().length()==0||tv8.getText().length()==0)
                {
                    Toast.makeText(getBaseContext(), "Please Enter all the fields!!",Toast.LENGTH_LONG).show();
                }
                else if(flag1==1)
                {

                    String sem= spinner2.getSelectedItem().toString();
                    String reason=e1.getText().toString();
                    String from=date1;
                    String to=date2;
                     AsyncTask<String,Void,String> t1=new LeaveTask(LeaveApp.this).execute(username,sem,reason,from,to);
                    try{
                        String result=t1.get();
                        Toast.makeText(getBaseContext(), result,Toast.LENGTH_LONG).show();
                    }catch(Exception e)
                    {

                    }
                     Intent i1=new Intent(LeaveApp.this,TestSelect.class);
                    i1.putExtra("data1", spinner2.getSelectedItem().toString());
                    startActivity(i1);
                }
               else if(flag2==1)
                {
                                   finish();
                }

                }
            });
       }


        public void addItemsOnSpinner2() {
            // TODO Auto-generated method stub
            spinner2=(Spinner) findViewById(R.id.spinner2);
            List<String> list2 = new ArrayList<String>();
            list2.add("I");
            list2.add("II");
            list2.add("III");
            list2.add("IV");
            list2.add("V");
            list2.add("VI");
            list2.add("VII");
            list2.add("VIII");

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list2);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(dataAdapter);
    }

        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_PICKER_ID1:


                // set date picker for current date 

                return new DatePickerDialog(this, pickerListener1, year, month,day);


           case DATE_PICKER_ID2:


                // set date picker for current date 

                return new DatePickerDialog(this, pickerListener2, year, month,day);


            }
            return null;
        }

        private DatePickerDialog.OnDateSetListener pickerListener1 = new DatePickerDialog.OnDateSetListener() {

            // when dialog box is closed, below method will be called.
            @Override
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {

                year  = selectedYear;
                month = selectedMonth;
                day   = selectedDay;

                 date1=""+day+"-"+month+"-"+year+" ";
                Output1.setText(new StringBuilder().append(day).append("-").append(month + 1)
                        .append("-").append(year).append(" "));


               }
            };
            private DatePickerDialog.OnDateSetListener pickerListener2 = new DatePickerDialog.OnDateSetListener() {

                // when dialog box is closed, below method will be called.
                @Override
                public void onDateSet(DatePicker view, int selectedYear,
                        int selectedMonth, int selectedDay) {

                    year  = selectedYear;
                    month = selectedMonth;
                    day   = selectedDay;

                    // Show selected date 
                     date2=""+day+"-"+month+"-"+year+" ";
                   Output2.setText(new StringBuilder().append(month + 1)
                            .append("-").append(day).append("-").append(year)
                            .append(" "));


                   }
                };
    }

// leavetask.java

package com.example.eleave;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
public class LeaveTask  extends AsyncTask<String,Void,String>{
    private Context context;
    public LeaveTask(Context context)
    {
        this.context=context;
    }
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
         try{
               String username=(String)arg0[0];
               String sem= (String)arg0[1];
               String reason=(String)arg0[2];
               String from=(String)arg0[3];
               String to=(String)arg0[4];
               String link = "http://trial.com/tl.php?username="
               +username+"&reason="+reason+"&from="+from+"&to="+to+"&sem="+sem;
               URL url = new URL(link);
               HttpClient client = new DefaultHttpClient();
               HttpGet request = new HttpGet();
               request.setURI(new URI(link));
               HttpResponse response = client.execute(request);
               BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
              StringBuffer sb = new StringBuffer("");
              String line="";
              while ((line = in.readLine()) != null) {
                 sb.append(line);
                 break;
               }
               in.close();
               return sb.toString();
         }catch(Exception e){
            return new String("Exception: " + e.getMessage());
         }
    }
    protected void onPreExecute(){
       }
}

// tl.php

<?php
$con=mysqli_connect("*****.com","*******","*******","********");
if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username=$_GET['username'];
$reason=$_GET['reason'];
$from=$_GET['from'];
$to=$_GET['to'];
$sem=$_GET['sem'];
$result = mysqli_query($con,"INSERT into test (phone_no,reason,from_date,to_date,sem) values('$username','$reason','$from','$to','$sem')");
echo $result;
mysqli_close($con);
?>

//错误 例外:索引82:// http://trial.com//tl.php?username=999999999&reason=ill&from=5-3-2014&to=6-3-14&sem=V

的查询中存在非法字符

1 个答案:

答案 0 :(得分:0)

尝试更换行

$result = mysqli_query($con,"INSERT into test (phone_no,reason,from_date,to_date,sem) values('$username','$reason','$from','$to','$sem')");

$result = mysqli_query($con,"INSERT into test (phone_no,reason,from_date,to_date,sem) values('$username','$reason','$from','$to','$sem')") or die("MYSQLi Error::".mysqli_error($con));