Php检查用户名和电子邮件(内部服务器错误)

Php检查用户名和电子邮件(内部服务器错误),php,android,Php,Android,当我在Eclipse中编码并连接到PHPMyAdmin中的数据库时,我有一个内部服务器错误。我遵循《人物》教程创建这个php连接。只是想知道我的php文件中是否有导致此错误的错误?我对PHP完全陌生 $hostname_localhost = "localhost"; $database_localhost = "Username"; $username_localhost = "Root"; $password_localhost = "Root"; $localhost = mysql_co

当我在Eclipse中编码并连接到PHPMyAdmin中的数据库时,我有一个内部服务器错误。我遵循《人物》教程创建这个php连接。只是想知道我的php文件中是否有导致此错误的错误?我对PHP完全陌生

$hostname_localhost = "localhost";
$database_localhost = "Username";
$username_localhost = "Root";
$password_localhost = "Root";
$localhost = mysql_connect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(), E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$username = $_POST['username'];
$password = $_POST['password'];
$email    = $_POST['email'];
$result   = "select * from tbl_user where username = '$username'";
mysql_query($result);

if (!$result) {
    die('Query fail to excute for some reason');
}

if (mysql_num_rows($result) > 0) {
    echo "Username Exist";
}

$result1 = "select * from tbl_user where user_email = '$email'";
mysql_query($result1);

if (!$result1) {
    die('Query fail to excute for some reason');
}

if (mysql_num_rows($result1) > 0) {
    echo "Email Exist";
}

$query_search = INSERT INTO `tbl_user`(`id`, `username`, `password`, `user_email`) VALUES('', '$username', '$password', 'email');
$query_exec = mysql_query($query_search) or die(mysql_error());
echo "User Added";  
对于我的android代码:

void registerUser(){
    try{                         
        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("http://yoururl.com/register.php"); // make sure the url is correct.
        //add your data
        nameValuePairs = new ArrayList<NameValuePair>(3);
        // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
        nameValuePairs.add(new BasicNameValuePair("username", usernameEditText.getText().toString()));// $Edittext_value = $_POST['Edittext_value'];
        nameValuePairs.add(new BasicNameValuePair("password", passwordEditText.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("email", emailaddressEditText.getText().toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
        response=httpclient.execute(httppost);
        // edited by James from coderzheaven.. from here....
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        System.out.println("Response : " + response); 
        runOnUiThread(new Runnable() {
            public void run() {
                //tv.setText("Response from PHP : " + response);
                dialog.dismiss();
            }
        });

        if(response.equalsIgnoreCase("User Added")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(RegisterActivity.this,"注册成功", Toast.LENGTH_SHORT).show();
                }

            });
            SharedPreferences settings = getSharedPreferences(STORED_USER, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("Username", usernameEditText.getText().toString());
            editor.commit();
            //Jump to next activity while intenting username over
            Intent intent = new Intent(RegisterActivity.this, LeftAndRightActivity.class);
            intent.putExtra(USER_NAME, usernameEditText.getText().toString());
            startActivity(intent);
        }else if(response.equalsIgnoreCase("Email Exist")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(RegisterActivity.this,"这邮箱已有账号", Toast.LENGTH_SHORT).show();
                }

            });

        }else if(response.equalsIgnoreCase("Username Exist")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(RegisterActivity.this,"这用户名已有人使用", Toast.LENGTH_SHORT).show();
                }

            });             
        }else{
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(RegisterActivity.this,response, Toast.LENGTH_SHORT).show();
                }
            });
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
你能试试这个吗,$sql=你的查询'$result=mysql\u query$sql

错误原因:您没有添加insert查询,即您的查询应该包装在引号中

$query_search = "INSERT INTO `tbl_user` (`id`, `username`, `password`, `user_email`) VALUES('', '$username', '$password', '$email')";
$query_exec = mysql_query($query_search) or die(mysql_error());
echo "User Added";  

注意:使用mysqli_*函数或PDO而不是mysqli_*函数预制的

能否请您提供错误消息和相关的代码部分。这意味着.PHP代码是正确的?是的,我们需要错误消息和导致错误的代码位。更新。谢谢请看一看:使用mysqli_*函数或PDO而不是mysql_*函数deprecatednope,奇怪的是当我从tbl_用户更改$result=select*时,其中username='NameinDatabase';它不会在我的浏览器中回显任何内容。我直接输入username@user1758158,我已经更新了我的答案,请检查是的,现在工作正常。但是,当我输入已经存在的用户名时,由于某种原因,当我运行registery的代码时,echo out out is Query无法执行。您的表有tbl_uu.prefix?-。为什么?是的,我现在有echo,但这不是我想要的echo。当我运行代码并注册一个新用户时,我输入用户名、密码和电子邮件,然后单击“注册”,它显示查询在某些情况下无法执行,并且没有插入到数据库中。我已经尝试过你的上述解决方案
    $sql  = "select * from tbl_user where username = '$username'";
    $result = mysql_query($sql) or die(mysql_error());


    if (mysql_num_rows($result) > 0) {
        echo "Username Exist";
    }

    $sql1 = "select * from tbl_user where user_email = '$email'";
    $result1 = mysql_query($sql1)  or die(mysql_error());   


    if (mysql_num_rows($result1) > 0) {
        echo "Email Exist";
    }
$query_search = "INSERT INTO `tbl_user` (`id`, `username`, `password`, `user_email`) VALUES('', '$username', '$password', '$email')";
$query_exec = mysql_query($query_search) or die(mysql_error());
echo "User Added";