2015-07-20 40 views
-2

我擁有將我的應用程序鏈接到我的sql數據庫的所有代碼logcat中沒有錯誤,應用程序不崩潰,並且在「數據輸入」的吐司消息在成功出現所有捕獲條款,但在數據庫表中沒有任何類型的數據輸入。在mysql數據庫中沒有輸入數據

這裏是MainActivity代碼:

public class MainActivity extends Activity { 

EditText name,age,email; 
Button b; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    setContentView(R.layout.activity_main); 
    name = (EditText) findViewById(R.id.name); 
    age = (EditText) findViewById(R.id.age); 
    email = (EditText) findViewById(R.id.email); 
    b = (Button) findViewById(R.id.done); 

    b.setOnClickListener(new View.OnClickListener() { 
     InputStream is = null; 
     @Override 
     public void onClick(View v) { 
      String username = name.getText().toString(); 
      String userage = age.getText().toString(); 
      String useremail = email.getText().toString(); 
      //setting the nameValuePair 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      nameValuePairs.add(new BasicNameValuePair("username",username)); 
      nameValuePairs.add(new BasicNameValuePair("userage",userage)); 
      nameValuePairs.add(new BasicNameValuePair("useremail",useremail)); 
      // 
      try{ 
       HttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost("http://sql17.000webhost.com/phpMyAdmin/index.php?db=a6923033_hamsau&lang=en-utf-8&token=4abc43994f0264c44701d85da5b524ee&phpMyAdmin=qsZJnJbsiQaPRx-aDIDtwuxOx5f,new.php"); 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity entity = httpResponse.getEntity(); 
       is = entity.getContent(); 

      } 
      catch (UnsupportedEncodingException e) { 
       Log.i("Hammas","UnsupportedEncodingException "+e); 
      } catch (ClientProtocolException e) { 
       Log.i("Hammas", "ClientProtocolException " + e); 
      } catch (IOException e) { 
       Log.i("Hammas", "IOException " + e); 

      } 
      Toast.makeText(getApplicationContext(),"Data is entered",Toast.LENGTH_SHORT).show(); 

     } 
    }); 

} 



} 

這裏是phpscript

enter code here 
<?php 
$mysql_host = "mysql17.000webhost.com"; 
$mysql_database = "a6923033_hamsau"; 
$mysql_user = "a6923033_hamsau"; 
$mysql_password = "123master95"; 

$name=$_POST['name']; 
$age=$_POST['age']; 
$email=$_POST['email']; 

mysql_query("insert into users(name,age,email)values('{$name}','{$age}',  {$email}')"); 

?> 

我認爲有錯誤的HTTP鏈接或phpscript,因爲這些都只是一些我沒了解清楚

+1

我當然希望那些不是你的實際DB憑證。如果他們是,你最好現在就去改變他們。 –

+0

你能否更清楚你的答案? – Usman

+0

你基本上只是把整個世界的鑰匙給了你的數據庫。你可能有一天醒來,發現一切已被刪除 – Osuwariboy

回答

-1

你的代碼是絕對正確和有效的,但有一點是你創建了具有相同名稱和相同列名的表。

+0

請提出代碼是如何完全正確和正常工作的,以及用相同名稱創建的表具有相同的列名? – Shehary

+0

好吧,我也有行'mysql_query(「插入到用戶(姓名,年齡,電子郵件)值('{$ name}','{$ age}',{$ email}')」)中的錯誤; '...但表名是'用戶'? – Usman

+0

@Usman,嘗試不帶'{name','$ age',$ email''的查詢 – Shehary