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

时间:2018-01-31 07:54:10

标签: java php

原谅我这个问题,但我是android开发的新手。所以我试图将TextView值放入我的URL,在我的webservice(PHP)中我有$ _GET来测试我的查询并只显示字段名称所需的匹配值“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 个答案:

答案 0 :(得分:1)

你必须这样做

如果id是数字:

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

如果id是String

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

否则id只是字符串“id”而不是您从GET检索的数字。 顺便说一下,由于SQL注入,这不是一个好习惯

在发布内容时始终隐藏您的密码和用户名

所以在Android中你错过了这个:

//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);