2017-09-23 44 views
-1

我正嘗試構建一個應用程序將我的圖書插入到Mysql數據庫中。我可以從應用程序中成功插入,但在我的數據庫中插入列名稱而不是值。Android應用程序插入列名而不是值到MySQL?

Addinfo.java

import android.app.Activity; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLEncoder; 

public class AddInfo extends Activity { 
    EditText Name,Email,Mobile; 
    String Bookname,Author,Status; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_info_layout); 
     Name=(EditText)findViewById(R.id.et_name); 
     Email=(EditText)findViewById(R.id.et_email); 
     Mobile=(EditText)findViewById(R.id.et_Mobile); 
    } 
    public void saveInfo (View view) 
    { 
     Bookname=Name.getText().toString(); 
     Author=Email.getText().toString(); 
     Status=Mobile.getText().toString(); 

     BackgroundTask backgroundTask = new BackgroundTask(); 
     backgroundTask.execute(Bookname,Author,Status); 
     finish(); 

    } 
    class BackgroundTask extends AsyncTask <String,Void,String> 
    { 
     String add_info_url; 
     @Override 
     protected void onPreExecute() { 
      add_info_url="http://abc.ga/StudentRegister.php"; 
      super.onPreExecute(); 
     } 

     @Override 
     protected String doInBackground(String... args) { 
      String name,email,mobile; 
      name=args[0]; 
      email=args[1]; 
      mobile=args[2]; 
      try { 
       URL url=new URL(add_info_url); 
       HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       OutputStream outputStream=httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8")); 
       String data_string = URLEncoder.encode("Bookname","UTF-8")+"="+URLEncoder.encode("Bookname","UTF-8")+ "&"+ 
         URLEncoder.encode("Author","UTF-8")+"="+URLEncoder.encode("Author","UTF-8")+ "&"+ 
         URLEncoder.encode("Status","UTF-8")+"="+URLEncoder.encode("Status","UTF-8"); 

        bufferedWriter.write(data_string); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       InputStream inputStream =httpURLConnection.getInputStream(); 
       inputStream.close(); 
       httpURLConnection.disconnect(); 
       return "Book Added Successfully"; 

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


      return null; 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      super.onProgressUpdate(values); 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show(); 
     } 
    } 


} 

StudentRegister.php

我創建了一個HTML文件來測試下面的PHP和它的作品不錯,但它從來沒有從Android的插入。

<?php 
    //StudentRegister.php 

if($_SERVER['REQUEST_METHOD']=='POST'){ 

include 'DatabaseConfig.php'; 

$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName); 
$Bookname=$_POST["Bookname"]; 
$Author=$_POST["Author"]; 
$Status=$_POST["Status"]; 
//$Date=$_POST["Date"]; 

$Sql_Query = "INSERT INTO Books values 
(DEFAULT,'$Bookname','$Author','$Status');"; 

if(mysqli_query($con,$Sql_Query)) 
{ 
echo 'Done'; 
} 
else 
{ 
echo 'Something went wrong'; 
} 
} 
mysqli_close($con); 
?> 

ERROR: Inserting column name and no values were inserting.

回答

0

上述問題得到了解決。

它是由URLENCODER引起的,並且在進行下面的修改之後它運行良好。

data_string = URLEncoder.encode("Bookname","UTF-8")+"="+URLEncoder.encode(Bookname,"UTF-8") 
相關問題