Jump to content

My query is being run with no results.


jamesxg1

Recommended Posts

Hiya peeps,

 

I have this.

 

    function DropUser($duser_id, $user_email, $user_username) {
        
    if(isset($_SESSION['admin_username']) && isset($_SESSION['admin_id']) && isset($_SESSION['admin_session_key'])) {
        
    $this->duser_id =  mysql_real_escape_string(trim(addslashes(strip_tags(is_numeric($duser_id)))));
    $this->user_email =  mysql_real_escape_string(trim(addslashes(strip_tags($user_email))));        
    $this->user_username =  mysql_real_escape_string(trim(addslashes(strip_tags($user_username))));        
       
    if(!empty($this->duser_id) && !empty($this->user_email) && !empty($this->user_username)) {
           
    $this->dropusersql = "DELETE FROM `members` WHERE `members`.`user_id` = '$this->duser_id' AND `members`.`username` = '$this->user_username' AND `members`.`emailaddress` = '$this->user_email' LIMIT 1";
    $this->run_dropusersql = mysql_query($this->dropusersql) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);    
         
    $this->removecomplete = '<font color="green"> - The user you select was removed.</font>';
    return($this->removecomplete);   
           
    } else {
           
    $this->emptyfields = "<font color='red'> - You have left mandatory fields empty.</font>";
    return($this->emptyfields);   
           
    }
        
    } else {
        
    header('Location: login.php');
        
        } 
       
    }

 

(Part of a class). I get the print back (<font color="green"> - The user you select was removed.</font>) but the query isnt deleting the row.

 

Many thanks,

 

James.

Link to comment
Share on other sites

Your code isn't actually testing the result of the query before it returns the "The user you select was removed" message and what trigger_error() does is dependent on the error_reporting level and what the display_errors/log_errors settings are.

 

You also don't need to use class variables for everything inside of a function unless you want or need to access those variables outside of the function.

 

Edit: Also, using both addslashes (which does not escape all the special characters that can break a query) and mysql_real_escape_string is double escaping your data and is wasting processing time. Remove the addslashes() function calls.

Link to comment
Share on other sites

Ok, so when you do your query you are issuing this:

 

$this->run_dropusersql = mysql_query($this->dropusersql) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

 

In this case, or trigger_error is never processed, because "or" has a lower operator precedence than the assignment "=" operator. 

 

http://us2.php.net/manual/en/language.operators.precedence.php

 

This is not unique by any means to PHP.  To get what it appears you want, which is to have the return value considered, you could use the higher precedence "||" operator, however, a clearer way to write this (practically out of the PHP manual) is to put in an if condition:

 

 

$this->run_dropusersql = mysql_query($this->dropusersql);
if (!$this->run_dropusersql) {
  trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);
} else {
  // Query worked ok.

 

With that said, there is an obvious question about your delete approach:

 

-If the user_id is unique (and it's the primary key of the table) why are you including other criteria?  All you should need to delete a user, is the user_id. 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.