SQL UserNames with Spaces Fix

Share your CuteNews.RU skins and code snippets.

SQL UserNames with Spaces Fix

Postby DarkSlim » Sat Aug 21, 2010 2:15 pm

Okay, here is a new fix that I found and havn't seen around, it was a very important
fix for me and I presume it will be usefull for many users. :)

I have seen some other users that bumped into similar problems in the past
with no good solution, so I'll explain now some of the problems it can solve and
how to apply the fix.

The Problem
In some cases / places in the code of CNR, it tries to find a specific username
in the users table (or in any other table). for example it searches: WHERE username = admin
The code as it is - works, but in some cases when the username has one or more spaces in it
it mostly WILL NOT be able to find it like this. for example: WHERE username = the admin
it will return nothing, so the parts of the code that use this way of searching will not work well
with all users.

Where can it be found?
I found it not working on user profiles, the code would not update the user's publications
and will not show the latest user posts - again, for user with spaces in their names.
It can be also found once on head.php and in other places.

* To check where it is found in your files, you can search your files for SQL queries
and see if it contains some form of "WHERE username = "
this will be mostly the case.


How to fix that?
To make the SQL understand the spaces we will need to edit the wanted username
before entering it to the query.
We will replace the spaces with % marks, which in a wildcard, means zero or more characters
and then in use the LIKE condition for it to work.

For example, open head and find this:
Code: Select all
   if ($action == 'dologin'){
      $sql->update(array(
      'table'    => 'users',
      'where'    => array("username = $username"),
      'values' => array('last_visit' => (time() + $config_date_adjust * 60))
      ));
   }


This is how you fix it to work with usernames that contain spaces:
Code: Select all
   if ($action == 'dologin'){
                $usrnm = str_replace(" ","%",$username);
      $sql->update(array(
      'table'    => 'users',
      'where'    => array("username LIKE $usrnm"),
      'values' => array('last_visit' => (time() + $config_date_adjust * 60))
      ));
   }



This is just an example of how it should be used, I tested the fix
in many parts of my installation with couple usernames with spaces
and it works great. ;)


Hope you'll find it usefull as I did, it is a very important fix for
our new community version that I would like to start working on
soon, if anyone can donate a hosting it would be great, I'll also try
asking Ramon. :)

DarkSlim
User avatar
DarkSlim
 
Posts: 298
Joined: Thu Aug 06, 2009 4:18 pm
Location: IL

Postby Hawk » Sun Aug 22, 2010 8:51 am

Very nice Darkslim!
User avatar
Hawk
 
Posts: 279
Joined: Tue Aug 29, 2006 9:00 pm


Return to Skins and snippets



Who is online

Users browsing this forum: No registered users and 0 guests

cron