简体   繁体   English

为什么不将其插入我的数据库?

[英]Why won't this insert into my database?

I am building and android app and want to submit this data into my online database. 我正在构建和Android应用程序,并希望将此数据提交到我的在线数据库中。 It executes the onPostExecute method so I know that it is getting called 它执行onPostExecute方法,所以我知道它正在被调用

However, when I look for the entry in my DB, it's not there. 但是,当我在数据库中查找条目时,它不存在。 Why is this? 为什么是这样?

Any help at all would be greatly appreciated. 任何帮助将不胜感激。

AsyncTask 异步任务

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

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

Error in the mysql_query function.Actually it is mysqli_query() when you use it with connection variable. mysql_query函数中的错误。与连接变量一起使用时,实际上是mysqli_query()

alternatively if you use mysql_query don't use connection variable $con . 或者,如果您使用mysql_query则不要使用连接变量$con

try 尝试

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

or 要么

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

It wasn't working because I wasn't meeting my own foreign key constraint in the database with my hard-coded values. 它没有用,因为我没有使用硬编码值满足数据库中自己的外键约束。 So I just changed it to real data and it worked like a champ. 所以我只是将其更改为真实数据,并且像冠军一样工作。

try this 尝试这个

use in activity 活动中使用

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

and in php file 并在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();

Where are you getting JSONParser from? 您从哪里获得JSONParser? I'm not finding it in the Android APIs for JSON . 我在JSONAndroid API中找不到它。 If you print out the JSON received from your request, I'd be guessing you're not actually making a legit call to start with. 如果您打印出从您的请求中接收到的JSON,我猜您实际上并没有进行合法的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM