2014-10-14 1 views
0

SQL 데이터베이스에서 일부 행을 업데이트하려고했습니다. php 함수는 잘 작동하지만 안드로이드 코드에 적용하면 아무 것도 바뀌지 않습니다. 내 응용 프로그램을 실행할 때 내 응용 프로그램은 "Update Successfull"이라고 말하지만 SQL 데이터베이스에서는 항상 동일합니다.android로 SQL 행을 업데이트하려고 시도했습니다.

누구든지 나를 도와 주거나 해결책을 찾도록 도와 줄 수 있습니까?

id | username | password | email | name 

PHP 함수의 코드 :

<?php 
if(isset($_POST['update'])) 
{ 
$dbhost = 'localhost'; 
$dbuser = 'dbuser'; 
$dbpass = 'dbpassword'; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass); 
if(! $conn) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

$username = $_POST['username']; 
$name = $_POST['name']; 

$sql = "UPDATE tbl_user SET name = '$name' WHERE username = '$username'"; 

mysql_select_db('my_table'); 
$retval = mysql_query($sql, $conn); 
if(! $retval) 
{ 
    die('Could not update data: ' . mysql_error()); 
} 
echo "Updated data successfully\n"; 
mysql_close($conn); 
} 
else 
{ 
?> 

안드로이드 활동의 코드 :

public class ProfileActivity extends Activity implements OnClickListener { 

    private EditText nome, user; 

    private Button update; 

    // Progress Dialog 
    private ProgressDialog pDialog; 

    // JSON parser class 
    JSONParser jsonParser = new JSONParser(); 


    //php login script 
    private static final String UPDATE_URL = "my_site/update.php"; 

    //ids 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_MESSAGE = "message"; 

    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_profile); 

     nome = (EditText) findViewById(R.id.editTextNomePersona); 
     user = (EditText) findViewById(R.id.editTextUserPersona); 

     update = (Button) findViewById(R.id.buttonUpdate); 
     update.setOnClickListener(this); 
    } 


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

       new UpdateUserProfile().execute(); 

    } 

    class UpdateUserProfile extends AsyncTask<String, String, String> { 


     boolean failure = false; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(ProfileActivity.this); 
      pDialog.setMessage(getResources().getString(R.string.update_attempt)); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... args) { 
      // TODO Auto-generated method stub 
      // Check for success tag 
      int success; 
      String name = nome.getText().toString(); 
      String username = user.getText().toString(); 

      try { 
       // Building Parameters 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("name", name)); 
       params.add(new BasicNameValuePair("username", username)); 

       Log.d("request!", "starting"); 
       // getting product details by making HTTP request 
       JSONObject json = jsonParser.makeHttpRequest(
         UPDATE_URL , "POST", params); 

       // check your log for json response 
       Log.d("Update attempt", json.toString()); 

       // json success tag 
       success = json.getInt(TAG_SUCCESS); 
       if (success == 1) { 
        Log.d(getResources().getString(R.string.update_successfull), json.toString()); 
        Intent i = new Intent(ProfileActivity.this, NewActivity.class); 

        finish(); 
        startActivity(i); 
        return json.getString(TAG_MESSAGE); 
       }else{ 
        Log.d(getResources().getString(R.string.update_failure), json.getString(TAG_MESSAGE)); 
        return json.getString(TAG_MESSAGE); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 

     } 
     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once product deleted 
      pDialog.dismiss(); 
      if (file_url != null){ 
       Toast.makeText(ProfileActivity.this, file_url, Toast.LENGTH_LONG).show(); 
      } 

     } 

    } 

} 

덕분에 많은 여기

그것은 MY_TABLE의 내 열입니다

activitiy_profile.xml :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 


      <TextView 
       android:id="@+id/textViewPersona" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="15dp" 
       android:layout_marginTop="25dp" 
       android:text="TextView" 
       android:textSize="60px" /> 

        <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_centerInParent="true" 
      android:orientation="horizontal" > 

      </RelativeLayout> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Nome Persona" /> 



     <EditText 
     android:id="@+id/editTextNomePersona" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

     <TextView 
      android:id="@+id/textView12" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Nome Utente" /> 
    <EditText 
     android:id="@+id/editTextUserPersona" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" /> 

    <Button 
     android:id="@+id/buttonUpdate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="UPDATE" /> 

    </LinearLayout> 

     </LinearLayout> 

    </ScrollView> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 
+0

PHP 스크립트에서 if (isset ($ _ POST [ 'update'])))를 테스트하고 있습니다. params.add (new BasicNameValuePair ("update", update));를 보내는 위치가 표시되지 않습니다. – eurosecom

답변

0

해결책을 찾았습니다. 이 하나

String name = nome.getText().toString(); 
    String username = user.getText().toString(); 

    try { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("name", name)); 
     params.add(new BasicNameValuePair("username", username)); 

:

<?php 

require("config.inc.php"); 

if (!empty($_POST)) { 

    if (empty($_POST['username']) || empty($_POST['nameperson'])) {   
     $response["success"] = 0; 
     $response["message"] = "Please Enter All Fields"; 
     die(json_encode($response)); 
    } 


    $query  = " SELECT 1 FROM tbl_user WHERE username = :user"; 
    $query_params = array(
     ':user' => $_POST['username'] 
    ); 

    try { 
     $stmt = $db->prepare($query); 
     $result = $stmt->execute($query_params); 
    } 
    catch (PDOException $ex) { 
     $response["success"] = 0; 
     $response["message"] = "Database Error1. Please Try Again!"; 
     die(json_encode($response)); 
    } 

    $query = "UPDATE tbl_user SET name = :nameperson WHERE username = :user"; 

    $query_params = array(
     ':user' => $_POST['username'], 
     ':nameperson' => $_POST['nameperson'] 
    ); 

    try { 
     $stmt = $db->prepare($query); 
     $result = $stmt->execute($query_params); 
    } 
    catch (PDOException $ex) { 
     $response["success"] = 0; 
     $response["message"] = "Database Error2. Please Try Again!"; 
     die(json_encode($response)); 
    } 

    $response["success"] = 1; 
    $response["message"] = "Username Successfully Added!"; 
    echo json_encode($response); 

} else { 
?> 

및 안드로이드 활동의 변화 :

나는

여기

이 그것을 작동하는 솔루션입니다 .... 그것은 미래의 누군가에 도움이 될 수 있습니다 희망

String nameperson = nome.getText().toString(); 
    String user = usern.getText().toString(); 

    try { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("nameperson", nameperson)); 
     params.add(new BasicNameValuePair("username", user)); 
관련 문제