目前,我从数据库中检索数据就像那样
private void getdatafromphp(){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/video.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
json_data = jArray.getJSONObject(jArray.length()-1);
url=json_data.getString("VideoUrl");
}catch(JSONException e1){
}catch(ParseException e1) {
e1.printStackTrace();
}
}
用这个php
<?php
mysql_connect("localhost","root","");
mysql_select_db("imammuda");
$sql=mysql_query("select * from Video");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
现在我想将数据插入数据库。怎么做?
我找到了sql命令,它是“插入表(column1,column2)的值('value1','value2')”。
这是带有常量值的插入,这是php中的类型。
我想要的是从java获得用户输入然后将此输入复制到php'value1'之后运行php来更新数据库。
答案 0 :(得分:0)
取决于您使用的是Get还是Post
我将假设GET
$value = $_GET['value']; // this will retrieve the value from the url and save it in a variable
mysql_connect("localhost","root","");
// escape the value first
$value = mysql_real_escape_string($value);
mysql_select_db("imammuda");
$result = mysql_query("insert into Video (value) values ('$value')");
?>
详细了解如何使用db here
<强>更新强>
知道正确的请求方法,你可以使用它。
$req;
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$req = $_GET;
}else {
$req = $_POST;
}
现在您可以使用$ req作为请求变量:
$value = $req['value'];