为什么获胜';不要把这个插入我的数据库


Why won't this insert into my database?

我正在构建一个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();
}
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')");

它不起作用,因为我的硬编码值没有满足数据库中自己的外键约束。所以我只是把它改成了真实的数据,它就像冠军一样发挥作用。

尝试这个

活动中使用

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

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