【问题标题】:Why won't this insert into my database?为什么这不会插入到我的数据库中?
【发布时间】:2013-12-14 02:50:55
【问题描述】:

我正在构建 Android 应用程序,并希望将此数据提交到我的在线数据库中。它执行onPostExecute 方法,所以我知道它被调用了

但是,当我在数据库中查找该条目时,它并不存在。这是为什么呢?

任何帮助都将不胜感激。

异步任务

private class SendTheYak extends AsyncTask<String, String, String>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    /**
     * Creating product
     * */
    @Override

    protected String doInBackground(String... args) {
        JSONParser jsonParser = new JSONParser();


        String message = newYak.getText().toString();


          // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.dichoapp.com/Chatter/sendMessage.php");

        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("userID", "William"));
            params.add(new BasicNameValuePair("lat", "35.5"));
            params.add(new BasicNameValuePair("long", "28.3"));
            params.add(new BasicNameValuePair("distance", "2"));
            params.add(new BasicNameValuePair("message", message));
            params.add(new BasicNameValuePair("handle", "android"));


            httppost.setEntity(new UrlEncodedFormEntity(params));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }



        return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        Toast.makeText(getApplicationContext(), "It sent to DB", Toast.LENGTH_LONG).show();
    }

}

php

<?php

    $DB_HostName = "localhost";
$DB_Name = "xxx";
$DB_User = "xxx";
$DB_Pass = "xxx";

$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 
mysql_select_db($DB_Name,$con) or die(mysql_error()); 


$userID = $_POST["userID"];
$lat = $_POST["lat"];
$long = $_POST["long"];
$distance = $_POST["distance"];
$message = $_POST["message"];
$message = mysql_real_escape_string($message);
$handle = $_POST["hndl"];
$handle = mysql_real_escape_string($handle);
$hidePin = $_POST["hidePin"];

if(empty($hidePin)){
    $hidePin = "0";
}


if(empty($handle)){
    mysql_query("INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$hidePin')", $con);
    $messageID = mysql_insert_id();

}else{
    mysql_query("INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, Handle, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$handle', '$hidePin')", $con);
    $messageID = mysql_insert_id();
}

【问题讨论】:

  • 收到什么错误?
  • 没有收到错误。什么都没有被推入数据库表中
  • 你能不能修改你的php脚本,在txt文件中写入日志,存储sql查询??
  • 不知道怎么做,能给我一个链接作为参考吗?
  • 我认为你应该使用method.equals("POST") 而不是method == "POST"

标签: java php android mysql


【解决方案1】:
mysql_query("INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$hidePin')", $con);

mysql_query 函数出错。实际上是mysqli_query() 与连接变量一起使用。

或者 如果您使用mysql_query,请不要使用连接变量$con

试试

mysql_query("INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$hidePin')");

mysqli_query($con,"INSERT INTO xxx (User_ID, Latitude, Longitude, Distance, Message, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$hidePin')");

【讨论】:

  • 你确定是这个问题吗?因为当我将它与 ios 应用程序一起使用时,相同的代码可以工作
  • 如果这是 PHP/JSON 连接,主要是 PHP 文件中会出现错误。我在连接 Android 手机时遇到过很多次。尝试 mysqli_query($con,"INSERT INTO xxx (User_ID, Latitude , 经度, 距离, 消息, HidePin) VALUES ('$userID', '$lat', '$long', '$distance', '$message', '$hidePin')");参考w3schools.com/php/func_mysqli_query.asp.I 已参考此链接以解决您的错误。
  • @WillJamieson: 你是在为 android 还是 iphone 开发。如果可能的话,把项目发给我。我可以帮你。邮件 id:arun.naagarajan@gmail.com
【解决方案2】:

它不起作用,因为我的硬编码值没有满足我自己在数据库中的外键约束。所以我只是将其更改为真实数据,它就像一个冠军一样工作。

【讨论】:

    【解决方案3】:

    试试这个

    在活动中使用

        nameValuePairs2 = new  ArrayList<NameValuePair>();
     nameValuePairs2.add(new BasicNameValuePair("username",username));
    nameValuePairs2.add(new BasicNameValuePair("password",password));
         try{
                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost(mainurl+"registration.php");
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs2));
                 HttpResponse response = httpclient.execute(httppost);
                 String the_string_response = convertResponseToString(response);
             //    Toast.makeText(getApplicationContext(), "Response " + the_string_response, Toast.LENGTH_LONG).show();
             }catch(Exception e){
                 //  Toast.makeText(getApplicationContext(), "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
                //   System.out.println("Error in http connection "+e.toString());
             } 
    

    在php文件中

    $sql="insert into login (username,password)values('".$_REQUEST['username']."','".$_REQUEST['password']."')";
     $r=mysql_query($sql);
     if(!$r)echo "Error in query: Record not submitted";
    else echo "data submitted successfully";
     mysql_close();
    

    【讨论】:

      【解决方案4】:

      你从哪里得到 JSONParser?我在Android APIs for JSON 中找不到它。如果您打印出从您的请求中收到的 JSON,我猜您实际上并没有从一开始就进行合法调用。

      【讨论】:

      • 这不是答案,是吗?
      猜你喜欢
      • 1970-01-01
      • 2021-12-16
      • 2019-09-07
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2013-06-22
      相关资源
      最近更新 更多