【问题标题】:Retrieving Android Data and passing it to to PHP WHERE Clause检索 Android 数据并将其传递给 PHP WHERE 子句
【发布时间】:2015-07-26 14:06:06
【问题描述】:

正如标题所说。我应该在我的 java 代码中包含哪些代码,以便我可以检索并将其传递给我的 PHP。

这是我的 PHP 代码

<?PHP
include_once("connection.php");

if(isset($_POST['TableNum'])){ $TableNum = $_POST['TableNum']; } 
$TableNum = null;
$sql = "SELECT Name, Price, orderlist.Quantity FROM menu INNER JOIN orderlist ON orderlist.MenuID=menu.MenuID WHERE orderlist.TableNum='+$TableNum+' "; 
$result = mysqli_query($con,$sql);
$json = array();

if(mysqli_num_rows($result)){
    while($row=mysqli_fetch_assoc($result)){
        $json['menu'][]=$row;
    }
}
mysqli_close($con);
echo json_encode($json);

这是我的 Java 代码

我需要将此变量作为我的 where 子句传递到我的 php 脚本中。

final TextView idtable = (TextView)findViewById(R.id.lblTableNum);

    final TextView idtable = (TextView)findViewById(R.id.lblTableNum);
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Orderlist.this);
    String aa = getResources().getString(R.string.Table_Num);
    String tableID = sp.getString(getString(R.string.Table_Num), aa);
    idtable.setText(tableID);

    ListView lv = (ListView) findViewById(R.id.listView1);      
    String url = "http://192.168.123.1/1purple/readOrder.php";
    try {
        JSONParser jParser = new JSONParser();
        JSONObject table = jParser.getJSONFromUrl(url);
        JSONArray data = table.getJSONArray("menu");
        //JSONArray data = new JSONArray(getJSONUrl(url));
        final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);

            map = new HashMap<String, String>();
            map.put("Name", c.getString("Name"));
            map.put("Quantity", c.getString("Quantity"));
            map.put("Price", c.getString("Price"));
            MyArrList.add(map);
        }
        SimpleAdapter sAdap;
        sAdap = new SimpleAdapter(Orderlist.this, MyArrList, R.layout.orderlist_arrangement,
                new String[] {"Name", "Quantity", "Price"}, new int[] {R.id.textView1, R.id.textView2, R.id.textView3});      
        lv.setAdapter(sAdap);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        Log.e("Log", "Failed" +e);
        e.printStackTrace();
    }           
 }

【问题讨论】:

  • 你想做什么?您想在调用 readOrder.php 文件时将参数从应用程序传递到服务器?
  • @galath 是的,我希望它是动态的,因此我想将所述变量传递给 php 文件

标签: java php android


【解决方案1】:

我遇到了和你一样的问题,我选择在 android 部分使用 volley 库,它帮助我将输入变量传递给 php where 子句 参考这篇文章并检查答案:

How to parse multiple rows with jsonOject

或按照下面的教程观看函数 function StoreUserInfo 并参考第二部分的活动名为 RegisterActivity.java 这些可以帮助您使用 where 子句获取:

https://www.androidtutorialpoint.com/androidwithphp/android-login-and-registration-with-php-mysql/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 2018-11-05
    • 2018-07-17
    相关资源
    最近更新 更多