du7535 2015-12-26 23:58
浏览 59
已采纳

无法从android php连接插入MySql Db

I'm making a little MySql Crud using Android-Studio and the data fetching is working just well but I cannot insert into db and I think the problem may be because of lack of permission to write (maybe?).

Anyway, in a given point, the user is at Register.java Activity and presses button to register. This calls the method onClick() on switch case R.id.BtRegs. This calls a registerUser method which calls the ServerRequests class. In the doInBackground I try to connect to my localhost using the IP of my network lan calling the Register.php with POST method. It does not return anything from Register.php. How do I check if it is indeed a lack of permission of some other thing? I think it may be no permission because the same methods used to fetch data worked well.

Here is the part of the code on ServerRequests class which does the connection:

@Override
    protected Void doInBackground(Void... params) {

        Map<String,String> dataToSend = new HashMap<>();


        dataToSend.put("name", user.name);
        dataToSend.put("age", String.valueOf(user.age));
        dataToSend.put("username", user.username);
        dataToSend.put("password", user.password);

        String encodedStr = getEncodedData(dataToSend);
        BufferedReader reader = null;

        try{

            URL url = new URL(SERVER_ADDRESS + "/Register.php");

            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            con.setConnectTimeout(CONNECTION_TIMEOUT);
            con.setReadTimeout(CONNECTION_TIMEOUT);

            con.setRequestMethod("POST");

            con.setDoOutput(true);
            OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());

            writer.write(encodedStr);
            writer.flush();

And this is the Register.php which does the MySQL insert:

<?php
$con = mysqli_connect("host", "user", "pass", "db");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO tabuser (name, age, username, password) VALUES (?,?,?,?) ");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
?>

note: Considering SERVER_ADDRESS = "http://192.168.x.x" and $con = mysqli_connect("host", "user", "pass", "db"); is normally set on main proj.

  • 写回答

1条回答 默认 最新

  • douzhengzuo7283 2016-03-17 15:29
    关注

    The problem is solved:

    As @Redbeard011010 commented, the lack of commit was causing this problem.

    So, on the code I added:

    $mysqli->autocommit(FALSE);
    //do the inserting
    if (!$mysqli->commit()) {
    print("Transaction commit failed
    ");
    exit();
    }
    

    After this, the code is working normally and the data is being inserted into the database. More info here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误