dongmi5020 2018-01-31 07: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-31 07: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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器