0

Android アクティビティからデータベースを作成しようとしていますが、テーブルに新しいアイテムを追加すると機能します。ただし、アイテムを更新しようとすると、いくつかの問題が発生し、これは機能しません。データベースの構造は、pid、name、price、および description です。データベースを作成するために、いくつかのスクリプトがあるサーバーにいくつかのパラメーターを投稿します。製品を作成するために、次のクエリを使用しますmysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");。これは正常に機能します。ただし、これを更新しようとすると、

 mysql_query("UPDATE products SET name = '$name', price = '$price', description = '$description' WHERE name = $name"); but this gives a JSON error.

このように pid だけでアイテムを更新することを考えましたがmysql_query("UPDATE products SET name = '$name', price = '$price', description = '$description' WHERE pid = $pid");、Android で pid の値を取得する人がわかりません。pid フィールドは Autoincrement であると言う必要があります。

編集:

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

                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(NumericoSup.this);
                    pDialog.setMessage("Creating Product..");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }

                /**
                 * Creating product
                 * */
                protected String doInBackground(String... args) {


                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("name", name1));
                    params.add(new BasicNameValuePair("price", "88"));
                    params.add(new BasicNameValuePair("description", "numericopr"));

                    // getting JSON Object
                    // Note that create product url accepts POST method
                    JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                            "POST", params);

                    // check log cat fro response
                    Log.d("Create Response", json.toString());

                    // check for success tag
                    try {
                        int success = json.getInt(TAG_SUCCESS);


                    } 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 done
                    pDialog.dismiss();
                }

            }   
            class SaveProductDetails extends AsyncTask<String, String, String> {

                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(NumericoSup.this);
                    pDialog.setMessage("Saving product ...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }

                /**
                 * Saving product
                 * */
                protected String doInBackground(String... args) {

                    // getting updated data from EditTexts


                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair(TAG_PID, "20"));
                    params.add(new BasicNameValuePair(TAG_NAME, name1));
                    params.add(new BasicNameValuePair(TAG_PRICE, "99"));
                    params.add(new BasicNameValuePair(TAG_DESCRIPTION, "update"));

                    // sending modified data through http request
                    // Notice that update product url accepts POST method
                    JSONObject json = jsonParser.makeHttpRequest(url_update_product,
                            "POST", params);

                    // check json success tag
                    try {
                        int success = json.getInt(TAG_SUCCESS);


                    } 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 uupdated
                    pDialog.dismiss();
                }
            }

Thanks in advance 
4

0 に答える 0