doujinge9648
2016-06-29 09:14
浏览 68

来自php mysql的Android响应代码200,但不在数据库中创建条目

I am sending data from my android app form to php mysql server and from server i am getting response code 200 but failed to make any entries into database.

Here is my php file where i am sending POST variables

    <?php

$host='localhost';
$uname='root';
$pass='pass';
$db='App';


$con=mysqli_connect($host,$uname,$pass,$db);


$First_Name=$_POST['First_Name'];
$Last_Name=$_POST['Last_Name'];
$Phone=$_POST['Phone'];
$Mail_ID=$_POST['Mail_ID'];
$Password =$_POST['Password'];
$La=$_POST['La'];
$Lo=$_POST['Lo'];
$Ac=$_POST['Ac'];
$Pro=$_POST['Pro'];

if($con){
 echo "Connected Successfully to database $First_Name "; //Print Variable to Check that file is getting inputs.Its Working.
}
else{
echo 'Failed To Connect to database ';
}


$sql="INSERT INTO `mobile_App`(`First_Name`, `Last_Name`, `Phone`, `Mail_ID`, `Password`, `La`, `Lo`, `Ac`, `Pro`)
VALUES ($First_Name,$Last_Name,$Phone,$Mail_ID,$Password,$La,$Lo,$Ac,$Pro)";

if(mysqli_query($con,$sql)){
echo 'Data Inserted Successfully';
}
else{
echo "ERROR: could not able to execute $sql.".
mysqli_error($con) ;
}

mysqli_close($con);
?>

This is my Java File

    public class Sending_Data_To_Server extends AsyncTask{
String Lo,La,Ac,Pro,First_Name,Last_Name,Phone,Mail_ID,Password;
BufferedReader reader;

@Override
protected String doInBackground(String... params) {
    //Variables
    First_Name=params[0];
    Last_Name=params[1];
    Phone=params[2];
    Mail_ID=params[3];
    Password=params[4];
    Lo=params[5];
    La=params[6];
    Ac=params[7];
    Pro=params[8];
    Log.d("SEND DATA VALUES",First_Name+Last_Name+Lo+La+Phone);


    try {
        String data= URLEncoder.encode("First_Name","UTF-8")+ "=" +URLEncoder.encode(First_Name,"UTF-8");
        data+= "&" +URLEncoder.encode("Last_Name","UTF-8")+ "=" +URLEncoder.encode(Last_Name,"UTF-8");
        data+= "&" + URLEncoder.encode("Phone","UTF-8")+ "=" +URLEncoder.encode(Phone,"UTF-8");
        data+= "&" + URLEncoder.encode("Mail_ID","UTF-8")+ "=" +URLEncoder.encode(Mail_ID,"UTF-8");
        data+= "&" + URLEncoder.encode("Password","UTF-8")+ "=" +URLEncoder.encode(Password,"UTF-8");
        data+= "&" + URLEncoder.encode("La","UTF-8")+ "=" +URLEncoder.encode(La,"UTF-8");
        data+= "&" + URLEncoder.encode("Lo","UTF-8")+ "=" +URLEncoder.encode(Lo,"UTF-8");
        data+= "&" + URLEncoder.encode("Ac","UTF-8")+ "=" +URLEncoder.encode(Ac,"UTF-8");
        data+= "&" + URLEncoder.encode("Pro","UTF-8")+ "=" +URLEncoder.encode(Pro,"UTF-8");



        URL url=new URL("test/Android/Data/data.php");   // can't post complete url due to restriction to new users 
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //For POST Only - Begin
        connection.setDoOutput(true);
        OutputStream os=connection.getOutputStream();
        BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
        writer.write(data);
        writer.flush();
        writer.close();
        os.close();
        connection.connect();
        //For POST Only End
        int responseCode=connection.getResponseCode();
        Log.d("Sending Class----","POST RESPONSE CODE "+responseCode);

        if (responseCode==HttpURLConnection.HTTP_OK){
            //Success
            reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response=new StringBuffer();

            while((inputLine=reader.readLine())!=null){
                response.append(inputLine);
            }
            reader.close();

            //Print Result
            Log.d("SENDING CLASS----",response.toString());

        }
        else{
            Log.d("SENDING CLASS---","POST did not work");

        }


    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    return "DATA SUBMITTED";
}

}

Here is Logcat

    06-29 20:48:52.703 7211-7211/com.boysjoys.com.pro_working1 D/REGISTRATION CLASS: ----ALL DATA READY AND CAPTURED
06-29 20:48:52.723 7211-29559/com.boysjoys.com.pro_working1 D/SEND DATA VALUES: helloworld28.531212977.25688999865321472
06-29 20:48:52.893 7211-7211/com.boysjoys.com.pro_working1 W/FragmentManager: moveToState: Fragment state for AppIntroFragment{423d9470 #1 id=0x7f0e009f android:switcher:2131624095:1} not updated inline; expected state 3 found 2
06-29 20:48:52.933 7211-7211/com.boysjoys.com.pro_working1 D/AppIntroBaseFragment: Slide Hello World has been selected.
06-29 20:48:53.423 7211-29559/com.boysjoys.com.pro_working1 D/Sending Class----: POST RESPONSE CODE 200
06-29 20:48:53.423 7211-29559/com.boysjoys.com.pro_working1 D/SENDING CLASS----: Connected Successfully to database hello ERROR: could not able to execute INSERT INTO `mobile_App`(`First_Name`, `Last_Name`, `Phone`, `Mail_ID`, `Password`, `La`, `Lo`, `Ac`, `Pro`)VALUES (hello,world,9810012345,Google@gmail.com,adffgghhjjk,77.2568899,28.5312129,37.5,Fixed).You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com,adffgghhjjk,77.2568899,28.5312129,37.5,fused)' at line 2

And my Database structure Rows Where I want to insert app data

Till now i have tried so many tutorials and guide and most of the places i have seen HttpClient which is depreciated so i am working with only HttpUrlConnection and as server returned status code 200 i think its some fault in php mysql file but not sure.

As @Adam Forbis suggest to print mysql error and check that value is getting by php file.So i did and i edit my PHP file to get those details and now i m updating logcat with mysql error

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dskyx46424 2016-06-29 15:56
    已采纳

    Maybe try as follow in you're php file (replace you're $sql var by this one):

    $sql="
        INSERT INTO `mobile_App`
        (
            `First_Name`,
            `Last_Name`,
            `Phone`,
            `Mail_ID`,
            `Password`,
            `La`,
            `Lo`,
            `Ac`,
            `Pro`
        )
        VALUES (
            '".$First_Name."',
            '".$Last_Name."',
            $Phone,
            '".$Mail_ID."',
            '".$Password."',
            $La,
            $Lo,
            $Ac,
            '".$Pro."'
        )
    ;";
    

    This is also the same answer at f_anto, but i confirm that you must have the single quote arround you're strings. (i only prefere write '".$var."' at '$var')

    this work perfectly for me (similar code) for testing you're problem :

    <?php
    
    
    $db = mysqli_connect("host", "userLogin", "userPwd", "dbName");
    
    $id = 999;
    $idc = 1;
    $idd = 0;
    $p = "gpog@email.com";
    $la = 15478.12;
    $l = 0;
    
    $sql="
            INSERT INTO `table`
            (
                `field1`,
                `field2`,
                `field3`,
                `field4`,
                `field5`,
                `field6`
            )
            VALUES (
                $id,
                $idc,
                $idd,
                '".$p."',
                $la,
                $l
            )
        ;";
    mysqli_query($db, $sql);
    
    var_dump(mysqli_error($db));
    
    点赞 评论
  • doujia1939 2016-06-29 09:17

    For the value of inserting query you should make like this (add quote in each value)

    $sql="INSERT INTO mobile_App(First_Name,Last_Name,Phone,Mail_ID,Password,La,Lo,Ac,Pro)
    VALUES ('$First_Name','$Last_Name','$Phone','$Mail_ID','$Password','$La','$Lo','$Ac','$Pro')";
    
    点赞 评论
  • dseomy1964 2016-06-29 09:24

    Use this Async task instead to call php api.

    public static class backgroundTask extends AsyncTask<String, String, String[]> {
        @Override
        protected void onPreExecute() {
            //do something before putting values in database 
        }
    
        @Override
        protected String[] doInBackground(String... f_url) {
            //call php api here with the arguments
    
            String url = "YOUR_PHP_API_URL_WITH_ARGUMENTS";
    
            HttpClient client = new DefaultHttpClient();
            try {
                client.execute(new HttpGet(url));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return f_url;
        }
    
        @Override
        protected void onPostExecute(String[] f_url) {
            //do something after putting values in database
        }
    }
    

    Also, make changes in your php api. Make it like this. I removed single quotes.

        $sql="INSERT INTO mobile_App(First_Name,Last_Name,Phone,Mail_ID,Password,La,Lo,Ac,Pro)
    VALUES ($First_Name,$Last_Name,$Phone,$Mail_ID,$Password,$La,$Lo,$Ac,$Pro)"; 
    
    点赞 评论

相关推荐 更多相似问题