如何通过用户输入将数据插入服务器数据库?

发布于 2024-11-08 05:29:00 字数 1876 浏览 0 评论 0原文

目前,我从数据库检索数据就像

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命令是“插入表(列1,列2)值('value1','value2')”。

这是在 php.ini 中插入常量值。

我想要的是从java那里获取用户的输入,然后将此输入复制到php“value1”中,然后运行php来更新数据库。

currently, i retrieve data from database is like that

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();
    }
}

with this 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();
?>

now i want insert data into database. how to do that?

I had found the sql command which is "insert into table (column1, column2) values ('value1', 'value2')".

This is insert with constant values which is type in php.

What i want is from java there get input from user then copy this input into php 'value1' after that run the php to update the database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2024-11-15 05:29:00

根据您使用的是 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 的更多信息

更新

要知道正确的请求方法,您可以使用此方法。

$req;
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $req = $_GET;
}else {
    $req = $_POST;
}

现在您可以使用 $req 作为请求变量:

$value = $req['value'];

Depending on whether you are using Get or Post

i will assume 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')");

?>

learn more about working with the db here

UPDATE

to know the correct request method you can use this.

$req;
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $req = $_GET;
}else {
    $req = $_POST;
}

now you can use $req as your request variable:

$value = $req['value'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文