Php 从android应用程序向mysql数据库插入数据
我正在尝试使用php文件从我的Android应用程序将数据插入mysql数据库。我不知道我做错了什么,这是我的android代码:Php 从android应用程序向mysql数据库插入数据,php,android,mysql,Php,Android,Mysql,我正在尝试使用php文件从我的Android应用程序将数据插入mysql数据库。我不知道我做错了什么,这是我的android代码: public void insert() { ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("nick",nick
public void insert()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("nick",nick));
nameValuePairs.add(new BasicNameValuePair("minuto",minuto));
nameValuePairs.add(new BasicNameValuePair("id_pelicula",String.valueOf(id_pelicula)));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxx.xxxxx.com/xxxx/insertar.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
Toast.makeText(getBaseContext(), "Opinión enviada correctamente",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Disculpe, inténtelo de nuevo",
Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
public void insert()
{
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“尼克”,尼克));
添加(新的BasicNameValuePair(“minuto”,minuto));
添加(新的BasicNameValuePair(“id_pelicula”,String.valueOf(id_pelicula));
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://xxxx.xxxxx.com/xxxx/insertar.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
Log.e(“通过1”,“连接成功”);
}
捕获(例外e)
{
Log.e(“Fail 1”,e.toString());
Toast.makeText(getApplicationContext(),“无效IP地址”,
Toast.LENGTH_LONG).show();
}
尝试
{
BufferedReader reader=新的BufferedReader
(新的InputStreamReader(is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
Log.e(“通过2”,“连接成功”);
}
捕获(例外e)
{
Log.e(“Fail 2”,e.toString());
}
尝试
{
JSONObject json_data=新的JSONObject(结果);
code=(json_data.getInt(“code”);
如果(代码==1)
{
Toast.makeText(getBaseContext(),“Opinión enviada correctamente”,
吐司。长度(短)。show();
}
其他的
{
Toast.makeText(getBaseContext(),“Disculpe,inténtelo de nuevo”,
Toast.LENGTH_LONG).show();
}
}
捕获(例外e)
{
Log.e(“Fail 3”,e.toString());
}
}
下面是我的php文件代码:
<?php
// Connection data
$host = 'xxx.xxx.com';;
$uname = 'uxxxx';
$pwd = 'xxxxx';
$db = 'xxxxx';
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$nick=$_REQUEST['nick'];
$minuto=$_REQUEST['minuto'];
$id_pelicula=$_REQUEST['id_pelicula'];
$flag['code']=0;
if($r=mysql_query("insert into opiniones values('$nick','$minuto','$id_pelicula') ",$con))
{
$flag['code']=1;
echo"hi";
}
print(json_encode($flag));
mysql_close($con);
?>
我已经解决了,这句话中的php文件中有一个错误:
if($r=mysql_query("insert into opiniones (nick, minuto, id_pelicula) values('$nick','$minuto','$id_pelicula') ",$con))
检查我的答案-->