我正面临一个WEIRD问题:我开发了一个需要注册的应用程序。
但是当在远程数据库(altervista)中保存用户名,邮件和密码时会出现问题:当我尝试查看用户的表时,只有空白记录。不为空,只是空白。
phpMyAdmin说有记录,但它们是空白的,即使我通过应用程序进行了注册......
没有异常抛出,没有网络错误,什么都没有。
我不知道是不是android代码不起作用,或者是php代码或其他东西。我正在这里粘贴他们,所以你可以帮助我(或者至少我希望你能帮助我)...
reg.setOnClickListener(new View.OnClickListener() { //reg is the registration button
public void onClick(View v) {
InputStream is;
if(username.getText().length()== 0)
{
username.setText("Enter Username");
}
else if(password.getText().length()== 0)
{
password.setText("Enter Password");
}
else if(email.getText().length()==0)
{
email.setText("Enter email");
}
else{
try{
String a = username.getText().toString().trim();
String b = password.getText().toString().trim();
String c = email.getText().toString().trim();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://tripleleon.altervista.org/register.php");
ArrayList<NameValuePair> dati = new ArrayList<NameValuePair>();
dati.add(new BasicNameValuePair("idutente",a));
dati.add(new BasicNameValuePair("password",b));
dati.add(new BasicNameValuePair("email", c));
httppost.setEntity(new UrlEncodedFormEntity(dati,HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
username.setText("");
password.setText("");
email.setText("");
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "Connection error",
Toast.LENGTH_SHORT).show();
}
}
}
});
这是php代码:
<?php
error_reporting(~E_NOTICE);
//include('db.php');
if($_GET['idutente'])
{
$fname=$_GET['idutente'];
//echo $fname.'gytgy';
}
if($_GET['password'])
{
$fname=$_GET['password'];
//echo $lname.'gytgy';
}
if($_GET['email'])
{
$email=$_GET['email'];
//echo $email.'fgfdgdf';
}
$conn=mysql_connect("ip","user","password");
if(!$conn)
{
die("could not connect". mysql_error());
}
mysql_select_db("my_tripleleon",$conn);
//$name =$_POST['name'];
$query="insert into info
(idutente, password, email)values('".$idutente."','".$password."','".$email."')";
$query1=mysql_query($query);
if ($query1="")
{echo "unsuccessfull";
}
else
{
echo"successfull";
}
?>
编辑:我没有使用线程或asynctasks。我只是(并且不那么正确)修改了ThreadPolicies,因为我为minSDK = 14制作了应用程序......
答案 0 :(得分:0)
尝试更像这样的东西(变量名称已被更改):
if($_GET['idutente'])
$idutente=$_GET['idutente'];
if($_GET['password'])
$password=$_GET['password'];
if($_GET['email'])
$email=$_GET['email']
$conn=mysql_connect("ip","user","password");
mysql_select_db("my_tripleleon",$conn);
$query="insert into info
(`idutente`, `password`, `email`)values('".$idutente."','".$password."','".$email."')";
另一件事是:
if ($query1="")
应该是:
if ($query1=="")
最后,您应该查看mysqli系列函数来替换您的“mysql_ *”调用。
答案 1 :(得分:0)
问题是你要从$ _GET数组中获取值,而你应该使用$ _POST数组,因为你使用的是HttpPost,而不是HttpGet。就这样。如果您愿意,可以使用$ _REQUEST,这样您就不必担心这些问题了。
顺便说一下......永远不要编写sql查询,因为你确实采用了用户传递的字符串。你必须逃避他们总是!!!
编辑以更好地解释:
执行此代码时:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://tripleleon.altervista.org/register.php");
ArrayList<NameValuePair> dati = new ArrayList<NameValuePair>();
dati.add(new BasicNameValuePair("idutente",a));
dati.add(new BasicNameValuePair("password",b));
dati.add(new BasicNameValuePair("email", c));
httppost.setEntity(new UrlEncodedFormEntity(dati,HTTP.UTF_8));
在register.php脚本中会发生这些值,这些值将存储在$ _POST变量中,而不是存储在$ _GET变量中,所以......
这将无效,您将获得一个空值(以及php日志中的警告):
$idutente=$_GET['idutente']
虽然这会起作用
$idutente=$_POST['idutente']
所以尝试让这些值访问$ _POST变量。