dongmi5020 2018-01-30 23:54
浏览 30
已采纳

如何将Textview值放入URL以使用$ _GET从PHP接收

Forgive me for asking this, But im new in android development. So I'm trying to put the TextView value into my URL, that in my webservice(PHP) I have $_GET to test my query and show only the desired match value of field name "WHERE CODE_ID = .$sample."

CameraTestActivity.class

if (!scanText.getText().toString().matches("")){

    //set the scan text from bar code label
    scanText.setText(sym.getData());

     //if Text scan the Content Automatic go to another Activity and pass the scan text from Main Activity

    Intent i = new Intent(CameraTestActivity.this, MainActivity2.class);
    i.putExtra("code_id", content);
    startActivity(i);
    finish();
}

MainActivity2.class

//trying to put the TextValue of scanText into tvView
tvView = (TextView)findViewById(R.id.textView9);
tvView.setText(getIntent().getExtras().getString("code_id"));

String ServerURL = ("https://asec-domain.000webhostapp.com/select.php?code_id="  + tvView);

getAll.php

if (isset($_GET['code_id'])) 
{ 
  $id  = $_GET['code_id'];
}  

// Create connection

$conn = new mysqli($HOST='localhost', $USER='id4400742_asec_domain', $PASS='asec@l0cal', $DB='id4400742_tbl_data');

if ($conn->connect_error) {

 die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM tbl_ComputerDetails WHERE CODE_ID= id "; 

$result = $conn->query($sql);

if ($result->num_rows >0) {

 while($row[] = $result->fetch_assoc()) {

 $tem = $row;

 $json = json_encode($tem);

 }

 echo $json;

 }

 else 
 {

 echo "No Results Found.";
 }
    $conn->close();
?>

展开全部

  • 写回答

1条回答 默认 最新

  • donglei1699 2018-01-30 23:58
    关注

    You have to do it like this

    If id is a number:

    $sql = "SELECT * FROM tbl_ComputerDetails WHERE CODE_ID = ".$id; 
    

    If id is a String

    $sql = "SELECT * FROM tbl_ComputerDetails WHERE CODE_ID = '".$id."'";
    

    Otherwise id is just the String "id" and not a number you retrieve from GET. And by the way this is not a good practice because of SQL Injection

    And always hide your passwords and usernames when you post something

    So in Android you are missing this:

    //trying to put the TextValue of scanText into tvView
    tvView = (TextView)findViewById(R.id.textView9);
    tvView.setText(getIntent().getExtras().getString("code_id"));
    
    //You are not getting the string you are just passing the textview
    String result = getIntent().getExtras().getString("code_id");
    
    String ServerURL = ("https://asec-domain.000webhostapp.com/select.php?code_id="  + result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing