0

我正在尝试将数据从 android 应用程序插入到 mysql 数据库表中。但不知何故数据没有被插入..我认为android和mysql数据库没有连接。我没有从 android 端收到任何错误。但在 php 方面...如果我运行 url .. 我会收到通知。那个未定义的变量。但这些变量在android中。

这是我的 android 端代码:

   package com.example.insertintophp;

import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText name,city,email,contact,msg;
    Button insert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        name =(EditText)findViewById(R.id.txtname);
        city =(EditText)findViewById(R.id.txtcity);
        email =(EditText)findViewById(R.id.txteid);
        contact =(EditText)findViewById(R.id.txtno);
        msg =(EditText)findViewById(R.id.txtmsg);
        insert=(Button)findViewById(R.id.btninsert);

        insert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0)
            {
                // TODO Auto-generated method stub
                String nm = name.getText().toString();
                String ct = city.getText().toString();
                String emailid = email.getText().toString();
                String no = contact.getText().toString();
                String str = msg.getText().toString();
                insertRecords(nm, ct, emailid, no, str);

            }
            private void insertRecords(String nm,String ct,String emailid,String no,String str)
            {
                 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
                    nameValuePairs.add(new BasicNameValuePair("name", nm));
                    nameValuePairs.add(new BasicNameValuePair("city",ct));
                    nameValuePairs.add(new BasicNameValuePair("email",emailid));
                    nameValuePairs.add(new BasicNameValuePair("contact",no));
                    nameValuePairs.add(new BasicNameValuePair("msg",str));
                    sendData(nameValuePairs);
            }
            private void sendData(ArrayList<NameValuePair> data)
            {
                try 
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://10.0.2.2:8080/demo/insertData.php");
                    httppost.setEntity(new UrlEncodedFormEntity(data));
                    HttpResponse response = httpclient.execute(httppost);

                }
                catch (Exception e) {
                    // TODO: handle exception
                    Log.e("log_tag", "Error:  "+e.toString());
                }
            }
        });

    }


}

这是我的 php 端代码:

  <?php 

$icon = mysql_connect("localhost","root",""); 
if(!$icon) 
{ 
die('Could not connect : ' . mysql_error()); 
} 
mysql_select_db("db_best_voyage", $icon)or die("database selection error"); 

echo json_encode($data); 
$name=$_POST['name']; 
$city=$_POST['city']; 
$email = $_POST['email']; 
$contact = $_POST['contact']; 
$msg = $_POST['msg']; 

mysql_query("INSERT INTO ctable (name,city,email,contact,msg) VALUES ('".$name."', '".$city."', '".$email."', '".$contact."', '".$msg."')"); 
mysql_close($icon); 

?>
4

2 回答 2

1

这是因为

1)您尚未声明/初始化任何 $data 变量,但您正在将其编码为 json 并回显。

2)此外,在访问 post 参数并将它们插入数据库后,您可以回显 jsonencoded 数据,如下所示。

    $name=$_POST['name']; 
    $city=$_POST['city']; 
    $email = $_POST['email']; 
    $contact = $_POST['contact']; 
    $msg = $_POST['msg']; 

    mysql_query("INSERT INTO ctable (name,city,email,contact,msg) VALUES ('".$name."', '".$city."', '".$email."', '".$contact."', '".$msg."')");

echo json_encode($data); 

确保向 $data 提供一些数据,以确保您成功插入记录。

编辑:

首先在您的浏览器中进行测试:

<?php 

$output = array();

$icon = mysql_connect("localhost","root",""); 
if(!$icon) 
{ 
die('Could not connect : ' . mysql_error()); 
} 
mysql_select_db("db_best_voyage", $icon)or die("database selection error"); 

echo json_encode($data); 
$name= "name1";
$city= "city1"; 
$email = "email1"; 
$contact = "contact1"; 
$msg = "msg1"; 

$insert1 = mysql_query("INSERT INTO ctable (name,city,email,contact,msg) VALUES ('".$name."', '".$city."', '".$email."', '".$contact."', '".$msg."')"); 
mysql_close($icon); 

if($insert1)
{
        $output["success"] = 1;
    $output["message"] = "Successfully inserted";

    // echoing JSON response
    echo json_encode($output);
}
else
{
     $output["success"] = 0;
    $output["message"] = "insertion failed......";

    // echoing JSON response
    echo json_encode($output);
}


?>
于 2013-01-02T12:17:19.740 回答
0

您的代码是完美的,但有些更改请尝试以下操作:

public class MainActivity extends Activity {

EditText name,city,email,contact,msg;
Button insert;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    name =(EditText)findViewById(R.id.txtname);
    city =(EditText)findViewById(R.id.txtcity);
    email =(EditText)findViewById(R.id.txteid);
    contact =(EditText)findViewById(R.id.txtno);
    msg =(EditText)findViewById(R.id.txtmsg);
    insert=(Button)findViewById(R.id.btninsert);

    insert.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            String nm = name.getText().toString();
            String ct = city.getText().toString();
            String emailid = email.getText().toString();
            String no = contact.getText().toString();
            String str = msg.getText().toString();

      insertRecords(nm,ct,emailid,no,str);
        }

    });
}
 private void insertRecords(String nm,String ct,String emailid,String no,String str)
        {
             ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
                nameValuePairs.add(new BasicNameValuePair("name", nm));
                nameValuePairs.add(new BasicNameValuePair("city",ct));
                nameValuePairs.add(new BasicNameValuePair("email",emailid));
                nameValuePairs.add(new BasicNameValuePair("contact",no));
                nameValuePairs.add(new BasicNameValuePair("msg",str));
                sendData(nameValuePairs);
        }
        private void sendData(ArrayList<NameValuePair> data)
        {
            try 
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2:8080/demo/insertData.php");
                httppost.setEntity(new UrlEncodedFormEntity(data));
                HttpResponse response = httpclient.execute(httppost);

            }
            catch (Exception e) {
                // TODO: handle exception
                Log.e("log_tag", "Error:  "+e.toString());
            }
        }
}
于 2013-01-02T10:24:03.980 回答