【发布时间】:2016-08-25 18:26:23
【问题描述】:
我的数据库中有一个整数,我正在尝试获取正确的列来显示数据库的其余部分
这就是我在 Android Studio 中的要求
public class SeeorderRequest extends StringRequest {
private static String SEEORDER_REQUEST_URL="http:Seeorder.php";
private Map<String, String> params;
public SeeorderRequest(String ordernum, Response.Listener<String> listener){
super(Method.POST, SEEORDER_REQUEST_URL, listener,null);
params= new HashMap<>();
params.put("ordernum",ordernum);
}
@Override
public Map<String, String> getParams() {
return params;
}
这是我的 php 代码
<?php
// array for JSON response
$response = array();
define('DB_USER', ""); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', ""); // database name
define('DB_SERVER', ""); // db server
// array for JSON response
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE);
// check for post data
if (isset($_GET["ordernum"])) {
$id = $_GET['ordernum'];
$sql = "SELECT * FROM orden WHERE orderid = $id";
$result = $conn->query($sql) or die (mysqli_connect_error());
if (!empty($result)) {
// check for empty result
if (mysqli_num_rows($result) > 0) {
$result = mysqli_fetch_array($result);
$orden = array();
$orden["ordernum"] = $result["orderid"];
$orden["pilotname"] = $result["pilotname"];
$orden["pilotcash"] = $result["pilotcash"];
$orden["date"] = $result["date"];
$orden["hoobsstart"] = $result["hoobsstart"];
$orden["hoobsend"] = $result["hoobsend"];
$orden["watchtime"] = $result["watchtime"];
$orden["hoobstime"] = $result["hoobstime"];
$orden["gas"] = $result["gas"];
$orden["liter"] = $result["liters"];
$orden["repairname"] = $result["repairname"];
$orden["repaircost"] = $result["repaircost"];
$orden["travelexpense"] = $result["patient_email"];
$orden["othername1"] = $result["othername1"];
$orden["othercost1"] = $result["othercost1"];
// success
$response["success"] = 1;
// user node
$response["myorder"] = array();
array_push($response["myorder"], $orden);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
问题在于,在我的数据库中,订单 ID 的值是一个自动增量 int,因此我无法使用此代码访问它,而来自 android 的 hashmap 仅接受字符串。
【问题讨论】:
-
警告:使用
mysqli时,您应该使用parameterized queries 和bind_param将用户数据添加到您的查询中。 请勿使用字符串插值或连接来完成此操作,因为您创建了一个严重的SQL injection bug。 切勿将$_POST或$_GET数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。 -
为什么无法访问?您仍然可以像使用任何其他列一样使用 auto_inc 列。他们只是在记录创建上有特殊的语义。
where auto_inc_field = $id完全有效。 -
所以我应该使用 where auto_inc_field = $id 而不是 WHERE orderid = $id" 即使 $id 是一个字符串值?
-
@MarcB 我收到此错误 b>Parse error: syntax error, unexpected T_STRING in /home/a2521927/public_html/androidphp/Seeorder.php 在线24
-
@tadman 任何关于参数化查询和 bind_param 的好教程你都知道吗?每当我发布问题时,您总是会发出警告 XD