2014-09-22 54 views
0

嗨在我的應用程序中我有一些學生姓名和滾號和點擊保存按鈕數據沒有插入數據庫和使用吐司消息它只顯示單個名稱和滾沒有什麼顯示和數據未插入。數據未能成功保存在Android中使用數據庫

Attend.class

public class Attend extends Activity implements OnClickListener { 

    String data = ""; 
    TableLayout tl; 
    TableRow tr; 
    TextView studentName; 
    EditText Firstname,stRoll; 
    CheckBox present; 
    String[] class_name, section_name; 
    String[] words; 
    String name; 
    String roll; 
    Button save; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tl = (TableLayout) findViewById(R.id.maintable); 

     save = (Button)findViewById(R.id.save); 
     Firstname = (EditText) findViewById(R.id.studentname); 
     Firstname.setVisibility(View.GONE); 
     stRoll = (EditText) findViewById(R.id.rollnumber); 
     stRoll.setVisibility(View.GONE); 
     save.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //save.setText("Record Saved Sucessfully"); 
       name = Firstname.getText().toString(); 
       System.out.println(name); 
       roll = stRoll.getText().toString(); 
       System.out.println(roll); 
       Toast.makeText(
         Attend.this, 
         "Result : " + "\nStudentName : " +name 
         + "\nRollno : " + roll, 

         Toast.LENGTH_SHORT).show(); 
       data = DatabaseUtility.executeQueryPhp("save",""); 
       System.out.println(data); 
      } 


     }); 

     String class_id = getIntent().getStringExtra("class_id"); 
     String section_id = getIntent().getStringExtra("section_id"); 
     Toast.makeText(
       this, 
       "Result : " + "\nclassName : " + class_id + "\nSectionName : " 
         + section_id, 

       Toast.LENGTH_SHORT).show(); 

     final String queryString = "class_id=" + class_id + "&section_id=" 
       + section_id; 
     /*String result = DatabaseUtility.executeQueryPhp("getStudent", 
       queryString);*/ 

     new Thread(new Runnable() { 
      public void run() { 
       data = DatabaseUtility.executeQueryPhp("getStudent",queryString); 
       System.out.println(data); 

       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         ArrayList<StudentDetails> users = parseJSON(data); 
         addData(users); 
        } 
       }); 

      } 
     }).start(); 

    } 
    public ArrayList<StudentDetails> parseJSON1(String result1) { 
     ArrayList<StudentDetails> users = new ArrayList<StudentDetails>(); 
     try { 
      JSONArray jArray = new JSONArray(result1); 
      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject json_data = jArray.getJSONObject(i); 
       StudentDetails user = new StudentDetails(); 

       user.setStudentname(json_data.getString("student_nme")); 
       user.setRollno(json_data.getString("roll_no")); 
       users.add(user); 
      } 
     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 
     return users; 
    } 

    public ArrayList<StudentDetails> parseJSON(String result) { 
     ArrayList<StudentDetails> users = new ArrayList<StudentDetails>(); 
     try { 
      JSONArray jArray = new JSONArray(result); 
      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject json_data = jArray.getJSONObject(i); 
       StudentDetails user = new StudentDetails(); 
       // user.setId(json_data.getInt("class_name")); 
       // user.setClass_id(json_data.getInt("section_name")); 
       // user.setSection_id(json_data.getInt("section_id")); 
       user.setStudentname(json_data.getString("first_name")); 
       user.setRollno(json_data.getString("roll_no")); 
       users.add(user); 
      } 
     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 
     return users; 
    } 

    void addHeader() { 

     tr = new TableRow(this); 

     studentName = new TextView(this); 


     studentName.setText("StudentName"); 

     studentName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     studentName.setPadding(5, 5, 5, 5); 
     studentName.setBackgroundColor(Color.RED); 
     LinearLayout Ll = new LinearLayout(this); 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
     params.setMargins(5, 5, 5, 5); 

     Ll.addView(studentName, params); 
     tr.addView((View) Ll); 

     TextView RollNo = new TextView(this); 
     RollNo.setText("RollNo"); 
     RollNo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     RollNo.setPadding(5, 5, 5, 5); 
     RollNo.setBackgroundColor(Color.RED); 
     Ll = new LinearLayout(this); 
     params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 5, 5, 5); 

     Ll.addView(RollNo, params); 
     tr.addView((View) Ll); 
     TextView Present = new TextView(this); 
     Present.setText("Present"); 
     Present.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     Present.setPadding(5, 5, 5, 5); 
     Present.setBackgroundColor(Color.RED); 
     Ll = new LinearLayout(this); 
     params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 5, 5, 5); 

     Ll.addView(Present, params); 
     tr.addView((View) Ll); 

     tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 



    } 

    @SuppressWarnings({ "rawtypes" }) 
    public void addData(ArrayList<StudentDetails> users) { 

     addHeader(); 

     for (Iterator i = users.iterator(); i.hasNext();) { 

      StudentDetails p = (StudentDetails) i.next(); 

      /** Create a TableRow dynamically **/ 
      tr = new TableRow(this); 

      /** Creating a TextView to add to the row **/ 
      Firstname = new EditText(this); 
      Firstname.setText(p.getStudentname()); 
      Firstname.setId(p.getId()); 
      Firstname.setLayoutParams(new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      Firstname.setPadding(5, 5, 5, 5); 

      LinearLayout Ll = new LinearLayout(this); 
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
      params.setMargins(5, 2, 2, 2); 

      Ll.addView(Firstname, params); 
      tr.addView((View) Ll); 

      EditText stRoll= new EditText(this); 
      stRoll.setText(p.getRollno()); 
      stRoll.setLayoutParams(new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      stRoll.setPadding(5, 5, 5, 5); 

      Ll = new LinearLayout(this); 
      params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 3, 3, 3); 

      Ll.addView(stRoll, params); 
      tr.addView((View) Ll); 
      CheckBox present = new CheckBox(this); 

      present.setLayoutParams(new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      present.setPadding(5, 5, 5, 5); 

      Ll = new LinearLayout(this); 
      params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 3, 3, 3); 

      Ll.addView(present, params); 
      tr.addView((View) Ll); 

      tl.addView(tr, new TableLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     } 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

    } 

save.php

<?php 

    mysql_connect("localhost","root",""); 
    mysql_select_db("login"); 
    $name = $_POST['name']; 
$roll = $_POST['roll']; 
    $q=mysql_query("insert into save (student_name,roll_no) values('$name','$roll')"); 

    while($row=mysql_fetch_assoc($q)) 
      $json_output[]=$row; 

    print(json_encode($json_output)); 

    mysql_close(); 

?> 
+1

有你測試它? 試圖使用postman(擴展到鉻) – 2014-09-22 12:31:47

+0

yes,但顯示未定義的json json_output在第13行C:\ xampp \ htdocs \ android_connect \ save.php null – user1 2014-09-22 12:36:13

+0

嘗試echo mysql_error(); 要得到錯誤消息:) – 2014-09-22 12:37:44

回答

0

你有這個錯誤是因爲沒有數組把數據($ json_output)

如果你定義$ json_output那裏應該沒有問題

save.php

<?php 

mysql_connect("localhost","root",""); 
mysql_select_db("login"); 
$name = !empty($_POST['name']) ? $_POST['name'] : $_GET['name']; 
$roll = !empty($_POST['roll']) ? $_POST['roll'] : $_GET['roll']; 
$query = "insert into save (student_name,roll_no) values('$name','$roll')"; 
$json_output = array(); 
if (!mysql_query($query)) { 
    $json_output[] = mysql_errno() . ": ".mysql_error(); 
}else{ 
    $json_output[] = true; 
} 

print(json_encode($json_output)); 
mysql_close(); 
?> 
+0

注意:未定義的索引:名稱在C:\ xampp \ htdocs \ android_connect \ save.php在線5 注意:未定義索引:第6行滾動到C:\ xampp \ htdocs \ android_connect \ save.php 警告:mysql_fetch_assoc()期望參數1是資源,布爾值在C:\ xampp \ htdocs \ android_connect \ save.php on line 9 [] – user1 2014-09-22 12:53:57

+0

它顯示此通知消息 – user1 2014-09-22 12:54:16

+0

如果你得到這些錯誤,那麼你的帖子是不正確:),因爲錯誤意味着你不把它作爲POST到服務器(正確得到) 我修改了我的答案,修復了一些問題其他的事情 – 2014-09-22 12:55:52

相關問題