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


Php Checking for username and Email (Internal server error)

在连接到PHPMyAdmin数据库的Eclipse中编码时,我遇到了内部服务器错误。我遵循人们关于建立此 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";  

对于我的安卓代码:

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

错误日志:

12-17 19:09:30.519: W/SingleClientConnManager(9526): Invalid use of    SingleClientConnManager: connection still allocated.
12-17 19:09:30.519: W/SingleClientConnManager(9526): Make sure to release the connection before allocating another one.

你能试试这个吗,$sql="your Query'; $result = mysql_query($sql);

    $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";  

注意:使用 mysqli_* 函数或 PDO 代替 mysql_* 函数(已弃用)