java 从 Android 应用程序将数据插入 MySQL 数据库。未反映在数据库中的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15728873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:39:58  来源:igfitidea点击:

Insert data into MySQL database from Android app. Changes not reflected in database

javaphpandroidmysqlinsert

提问by user2201650

I'm writing an application to insert data into a MySQL database. I wrote the sql query in PHP and when I enter values in the android application, I don't seen any errors in the log cat but the values aren't getting stored in the database.

我正在编写一个应用程序来将数据插入到 MySQL 数据库中。我在 PHP 中编写了 sql 查询,当我在 android 应用程序中输入值时,我在日志 cat 中没有看到任何错误,但这些值没有存储在数据库中。

Here's the PHP code:

这是PHP代码:

<?php

$connect = mysqli_connect("localhost","root","","easyassigndroid");

if(mysqli_connect_errno($connect))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
    echo "success";
}

$username = isset($_POST['sPhone']) ? $_POST['sPhone'] : '';
$password = isset($_POST['sPassword']) ? $_POST['sPassword'] : '';

$query = mysqli_query($connect, "insert into users (sphone, spassword) values ('$username' ,'$password') ");

mysqli_close($connect);
?>

Here's the android part:

这是安卓部分:

protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.register_lecturer);

    phonenum = (EditText) findViewById(R.id.id_phone);
    password = (EditText) findViewById(R.id.id_password);

    sPhone = phonenum.toString();
    sPassword = password.toString();

    registerLecturer=(Button)findViewById(R.id.lecturerregister);
    registerLecturer.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://10.0.2.2/project/insert.php");

            try
            {
                nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("sphone", sPhone));
                nameValuePairs.add(new BasicNameValuePair("spassword", sPassword));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpclient.execute(httppost);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    });


}

I'm a beginner to both Android and PHP coding. Can anyone tell me where the problem is in my code? I need this for my project work.

我是 Android 和 PHP 编码的初学者。谁能告诉我问题出在我的代码中?我的项目工作需要这个。

采纳答案by d'alar'cop

Just one point: to get the value from an Android EditText we use

只有一点:从我们使用的 Android EditText 中获取值

x.getText().toString(); 

where x is an EditText object, right? hope it helps at all.

其中 x 是一个 EditText 对象,对吗?希望它有帮助。

But, since this is being done in the onCreate() - rather than inside the onClick() - you should change:

但是,由于这是在 onCreate() 中完成的 - 而不是在 onClick() 中 - 您应该更改:

nameValuePairs.add(new BasicNameValuePair("sphone", sPhone));

to

nameValuePairs.add(new BasicNameValuePair("sphone", phonenum.getText().toString()));

And similarly for the other one. Cheers.

另一个类似。干杯。

回答by Martin Perry

As I can see, you ar using "sPhone" and "sphone" as keys. I would recommend to have it both with same names.

正如我所看到的,您使用“sPhone”和“sphone”作为键。我建议两者都使用相同的名称。

nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("sphone", sPhone));
nameValuePairs.add(new BasicNameValuePair("spassword", sPassword));

And in PHP

在 PHP 中

$_POST['sphone']
$_POST['spassword']