user1758158 user1758158 - 2 months ago 10
PHP Question

Php Checking for username and Email (Internal server error)

While coding in my Eclipse connecting to my database in PHPMyAdmin I have an internal server error. I follow people tutorial on making this php connection. Just wondering is there any mistake in my

php
file that causes this error? I am completely new to 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";


For my android code:

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


Error Log:

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.

Answer

Can you try this, $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";
    }

The reason for the error : You have missed to add " in insert query, i.e. your query should be wrapped inside the quotes.

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

Note: Use mysqli_* functions or PDO instead of mysql_* functions(deprecated)

Comments