gpt4 book ai didi

php - 从android应用程序向mysql数据库插入数据

转载 作者:行者123 更新时间:2023-11-30 23:01:40 24 4
gpt4 key购买 nike

xxx我正在尝试使用 php 文件从我的 Android 应用程序将数据插入 mysql 数据库。我不知道我做错了什么,这是我的 android 代码:

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

这是我的 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);
?>

可能是因为在我的 mysql 数据库中,“minuto”变量是整数,而我使用类似 android 中的字符串?请问有人可以帮助我吗?非常感谢

最佳答案

我已经解决了,我这句话在php文件中出错了:

if($r=mysql_query("insert into opiniones (nick, minuto, id_pelicula) values('$nick','$minuto','$id_pelicula') ",$con))

关于php - 从android应用程序向mysql数据库插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com