嗨,大家搜索了2-3天后,我完全不知道出了什么问题,所以我很感激一些帮助:)
我正在使用wampserver ..我遇到问题是从android中将数据导入mysql ..每当我按下插入按钮时,我的列中都会添加一个空白行而没有任何信息。
这是我的插入
的php<?php
$abc=$_POST['abc'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbinsert", $con);
$sql="INSERT INTO Detail (Utext) VALUES ('".$abc."')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
for my android
package com.project.insert;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
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.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PassData extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Insert=(Button)findViewById(R.id.button1);
Insert.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
try
{
EditText CompleteText=(EditText)findViewById(R.id.editText1);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("abc", "'"+CompleteText.getText().toString()+"'"));
Log.e(""+CompleteText.getText().toString(),"0");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/InsertStatement.php");
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
});
}
}
答案 0 :(得分:2)
我没有看到您在nameValuePairs
处执行任何操作以将其放入请求中。
HttpPost httppost = new HttpPost("http://10.0.2.2/InsertStatement.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
可能会成功。
出于安全原因,您可能希望在服务器上添加'
(并且不安卓代码 - 现在您使用两者都会显示mysql错误)并使用{ {1}}服务器端。