2016-04-22 44 views
0

我不能保存選定的單選按鈕和微調到MySQL數據庫?爲什麼是這樣?但對於editText,它保存到我的數據庫。請幫助! 這裏是我的submitData.php如何保存微調數據和選定的單選按鈕保存到MySQL數據庫?

<html> 
    <body> 
     <form action="submitData" method = "post"> 
     Gender <input type = "radio" name="gender" value ="<?php echo $row['Gender']?>"> 
       <input type = "radio" name="gender" value ="<?php echo $row['Gender']?>" 
     </form> 
    </body> 
</html> 

<?php 

if($_SERVER['REQUEST_METHOD']=='POST'){ 
$Name = $_POST['Name']; 
$Age = $_POST['Age']; 
$Gender=$_POST['Gender']; 
$Email = $_POST['Email']; 
$Phone = $_POST['Phone']; 
$IncidentType = $_POST['IncidentType']; 
$Description = $_POST['Description']; 

require_once('dbConnect.php'); 

$sql = "INSERT INTO user (Name,Age,Gender,Email,Phone,IncidentType,Description) VALUES ('$Name','$Age','$Gender','$Email','$Phone','$IncidentType','$Description')"; 


if(mysqli_query($con,$sql)){ 
echo "Successfully sent"; 
}else{ 
echo "Could not send"; 

} 
}else{ 
echo 'error'; 
} 
?> 

這裏是我的FormActivity.java:

public class FormActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener { 

    private static final String REGISTER_URL = "http://khaty-ismail0.rhcloud.com/phptutorial/submitData.php"; 
    private static final String SPINNER_URL = "http://khaty-ismail0.rhcloud.com/phptutorial/spinner_data.php"; 

    public static final String KEY_NAME= "Name"; 
    public static final String KEY_AGE = "Age"; 
    public static final String KEY_EMAIL = "Email"; 
    public static final String KEY_PHONE = "Phone"; 
    public static final String KEY_DESCRIPTION = "Description"; 
    public static final String KEY_TICKET = "ticket_id"; 
    public static final String KEY_SOLUTION = "solution"; 
    public static final String JSON_ARRAY = "result"; 

    private EditText name_eT; 
    private EditText age_eT; 
    private EditText email_eT; 
    private EditText phone_eT; 
    private EditText descr_eT; 

    private Button subm_btn; 
    private Spinner spinner; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_form); 

     name_eT = (EditText) findViewById(R.id.name_eT); 
     age_eT = (EditText) findViewById(R.id.age_eT); 
     email_eT= (EditText) findViewById(R.id.email_eT); 
     phone_eT= (EditText) findViewById(R.id.phone_eT); 
     descr_eT= (EditText) findViewById(R.id.descr_eT); 

     subm_btn = (Button) findViewById(R.id.subm_btn); 

     subm_btn.setOnClickListener(this); 

     spinner = (Spinner) findViewById(R.id.spinner); 
     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(getBaseContext(), spinner.getSelectedItem().toString(), 
         Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 
    } 
    private void registerUser(){ 
     final String Name = name_eT.getText().toString().trim(); 
     final String Age = age_eT.getText().toString().trim(); 
     final String Email = email_eT.getText().toString().trim(); 
     final String Phone = phone_eT.getText().toString().trim(); 
     final String Description = descr_eT.getText().toString().trim(); 

     if(TextUtils.isEmpty(Name)) { 
      name_eT.setError("Please enter your name"); 
      return; 
     }else if(TextUtils.isEmpty(Age)) { 
      age_eT.setError("Please enter your age"); 
      return; 
     }else if(TextUtils.isEmpty(Phone)) { 
      phone_eT.setError("Please enter your phone"); 
      return; 
     }else if(TextUtils.isEmpty(Email)) { 
      email_eT.setError("Please enter your email"); 
      return; 
     }else if(TextUtils.isEmpty(Description)) { 
      descr_eT.setError("Please enter your issue"); 
      return; 
     } 
     StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         Toast.makeText(FormActivity.this,response,Toast.LENGTH_LONG).show(); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(FormActivity.this,error.toString(),Toast.LENGTH_LONG).show(); 
        } 
       }){ 
      @Override 
      protected Map<String,String> getParams(){ 
       Map<String,String> params = new HashMap<String, String>(); 
       params.put(KEY_NAME,Name); 
       params.put(KEY_AGE,Age); 
       params.put(KEY_EMAIL, Email); 
       params.put(KEY_PHONE, Phone); 
       params.put(KEY_DESCRIPTION, Description); 
       return params; 
      } 

     }; 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 
    } 
    public void onCheckboxClicked(View V) { 

     boolean checked = ((RadioButton) V).isChecked(); 

     switch (V.getId()) { 
      case R.id.male_rb: 
       if (checked) 

        break; 

      case R.id.female_rb: 
       if (checked) 

        break; 
     } 
    } 

    @Override 
    public void onClick(View v) { 
     if(v == subm_btn){ 
      registerUser(); 
     } 
    } 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
    } 

    public void submitData(View view) { 
     Intent getResponse = new Intent(this,SubmitData.class); 

     final int result = 1; 

     startActivityForResult(getResponse, result); 
    } 
} 

這裏是我的activity_form.java:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:background="@color/buttonFont" 
    tools:context="com.example.khadijah.brucertv2.FormActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:text="@string/User_info" 
     android:id="@+id/userInfo" 
     android:textSize="30sp" 
     android:textColor="@color/colorFont" 
     android:textStyle="bold" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"/> 

    <TextView 
     android:layout_width="70dp" 
     android:layout_height="wrap_content" 
     android:text="Name:" 
     android:id="@+id/name_tV" 
     android:textColor="@color/colorFont" 
     android:layout_marginTop="24dp" 
     android:layout_below="@+id/userInfo" 
     android:layout_alignLeft="@+id/userInfo" 
     android:layout_alignStart="@+id/userInfo" /> 

    <TextView 
     android:layout_width="70dp" 
     android:layout_height="wrap_content" 
     android:text="Age:" 
     android:id="@+id/age_tV" 
     android:textColor="@color/colorFont" 
     android:layout_alignBottom="@+id/age_eT" 
     android:layout_alignLeft="@+id/name_tV" 
     android:layout_alignStart="@+id/name_tV" /> 

    <TextView 
     android:layout_width="70dp" 
     android:layout_height="wrap_content" 
     android:text="Gender" 
     android:id="@+id/gender_tV" 
     android:textColor="@color/colorFont" 
     android:layout_below="@+id/age_tV" 
     android:layout_alignLeft="@+id/age_tV" 
     android:layout_alignStart="@+id/age_tV" 
     android:layout_marginTop="24dp" /> 

    <TextView 
     android:layout_width="70dp" 
     android:layout_height="wrap_content" 
     android:text="Email:" 
     android:id="@+id/email_tV" 
     android:textColor="@color/colorFont" 
     android:layout_alignBottom="@+id/email_eT" 
     android:layout_alignLeft="@+id/name_tV" 
     android:layout_alignStart="@+id/name_tV" /> 

    <TextView 
     android:layout_width="70dp" 
     android:layout_height="wrap_content" 
     android:text="Phone" 
     android:id="@+id/phone_tV" 
     android:textColor="@color/colorFont" 
     android:layout_alignBottom="@+id/phone_eT" 
     android:layout_alignLeft="@+id/gender_tV" 
     android:layout_alignStart="@+id/gender_tV" /> 

    <EditText 
     android:layout_width="200dp" 
     android:layout_height="30dp" 
     android:layout_marginBottom="5dp" 
     android:inputType="textPersonName" 
     android:hint="@string/hint_required" 
     android:textColorHint="@color/buttonFont" 
     android:background="@drawable/border_style" 
     android:ems="10" 
     android:id="@+id/name_eT" 
     android:textColor="@color/inputFont" 
     android:layout_alignBottom="@+id/name_tV" 
     android:layout_alignRight="@+id/userInfo" 
     android:layout_alignEnd="@+id/userInfo" /> 

    <EditText 
     android:layout_width="200dp" 
     android:layout_height="30dp" 
     android:inputType="number" 
     android:ems="10" 
     android:hint="@string/hint_required" 
     android:textColorHint="@color/buttonFont" 
     android:background="@drawable/border_style" 
     android:id="@+id/age_eT" 
     android:textColor="@color/inputFont" 
     android:layout_below="@+id/name_tV" 
     android:layout_alignRight="@+id/userInfo" 
     android:layout_alignEnd="@+id/userInfo" /> 

    <EditText 
     android:layout_width="200dp" 
     android:layout_height="30dp" 
     android:inputType="textEmailAddress" 
     android:ems="10" 
     android:id="@+id/email_eT" 
     android:hint="@string/hint_required" 
     android:textColorHint="@color/buttonFont" 
     android:textColor="@color/inputFont" 
     android:background="@drawable/border_style" 
     android:layout_below="@+id/phone_eT" 
     android:layout_alignLeft="@+id/age_eT" 
     android:layout_alignStart="@+id/age_eT" /> 

    <EditText 
     android:layout_width="200dp" 
     android:layout_height="30dp" 
     android:inputType="phone" 
     android:ems="10" 
     android:id="@+id/phone_eT" 
     android:layout_marginBottom="5dp" 
     android:textColor="@color/inputFont" 
     android:hint="@string/hint_required" 
     android:textColorHint="@color/buttonFont" 
     android:background="@drawable/border_style" 
     android:layout_below="@+id/gender_tV" 
     android:layout_alignLeft="@+id/email_eT" 
     android:layout_alignStart="@+id/email_eT" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:text="@string/incident_info" 
     android:id="@+id/incidentInfo" 
     android:textColor="@color/colorFont" 
     android:textStyle="bold" 
     android:textSize="30sp" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

    <Spinner 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/spinner" 
     android:prompt="@array/incident_type" 
     android:entries="@array/incident_type" 
     android:layout_below="@+id/incidentInfo" 
     android:layout_alignLeft="@+id/phone_eT" 
     android:layout_alignStart="@+id/phone_eT" 
     android:layout_alignRight="@+id/phone_eT" 
     android:layout_alignEnd="@+id/phone_eT" 
     android:spinnerMode="dropdown" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:text="@string/incident_type_DD" 
     android:textColor="@color/colorFont" 
     android:id="@+id/incident_DD" 
     android:layout_below="@+id/incidentInfo" 
     android:layout_toLeftOf="@+id/spinner" 
     android:layout_toStartOf="@+id/spinner" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:text="@string/description" 
     android:id="@+id/description_tV" 
     android:textColor="@color/colorFont" 
     android:layout_below="@+id/spinner" 
     android:layout_alignLeft="@+id/phone_tV" 
     android:layout_alignStart="@+id/phone_tV" /> 

    <EditText 
     android:layout_width="300dp" 
     android:layout_height="80dp" 
     android:inputType="textMultiLine" 
     android:ems="10" 
     android:id="@+id/descr_eT" 
     android:hint="@string/descr_hint" 
     android:textColorHint="@color/buttonFont" 
     android:gravity="top" 
     android:background="@drawable/border_style" 
     android:textColor="@color/inputFont" 
     android:layout_below="@+id/description_tV" 
     android:layout_alignLeft="@+id/description_tV" 
     android:layout_alignStart="@+id/description_tV" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:layout_margin="5dp" 
     android:text="SUBMIT" 
     android:onClick="submitData" 
     android:textStyle="bold" 
     android:textSize="35sp" 
     android:id="@+id/subm_btn" 
     android:background="@drawable/custom_btn" 
     android:textColor="@color/buttonFont" 
     android:layout_alignParentBottom="true"/> 

    <RadioGroup 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_alignLeft="@+id/phone_eT" 
     android:layout_alignStart="@+id/phone_eT" 
     android:layout_below="@+id/age_eT" 
     android:layout_alignRight="@+id/descr_eT" 
     android:layout_alignEnd="@+id/descr_eT" 
     android:layout_above="@+id/phone_eT" 
     android:weightSum="1" 
     android:id="@+id/radioGroup"> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="false" 
      android:text="Male" 
      android:id="@+id/male_rb" 
      android:onClick="onCheckboxClicked"/> 
     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Female" 
      android:checked="false" 
      android:id="@+id/female_rb" 
      android:onClick="onCheckboxClicked"/> 

    </RadioGroup> 


</RelativeLayout> 

這裏是我的arrays.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string-array name="incident_type"> 
     <item>Fraud</item> 
     <item>Malicious Code</item> 
     <item>Spam</item> 
     <item>Intrusion</item> 
     <item>Not Sure</item> 
    </string-array> 


</resources> 

回答

0

t他的名字是大小寫敏感的。 使用$ _POST ['gender'];

另一件事是。你在哪裏定義$ row?看起來可能是空的,所以你不會發布任何數據。

啓用的error_reporting和display_errors設置,而發展幫助你找到這類錯誤的快得多

另一件事很重要不要使用mysql_ *功能,它們是不安全的,不推薦使用。看看廣告PDO而不是

也使用準備的語句和轉義userinput。

+0

嗨,我仍然無法保存性別選定的單選按鈕。有沒有我的PHP文件上面有任何錯誤?已經更新$ _POST ['gender']。 – user6181358

+0

作爲說啓用錯誤報告..什麼是值?<?php echo $ row ['Gender']?> – di3

+0

$ row = mysql_fetch_array ...但我不知道如何編寫php..any幫助?請..性別是男性還是女性 – user6181358

相關問題