SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    82
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Problem with & + Database

    Hello
    I'm using PHP to insert names to my databae and some of the names contains "&" symbol.
    When Im trying to access the name page (like mysite.com/hocky&tocky) it cannot find the specific name from the database (it does not recognize hocky&tocky).
    How I can fix that and still show the "&" symbol?
    Owner of uReviews & MovieDB - Movie Database

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Replace & with & or &

    Look at the urlencode and urldecode functions.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    82
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    Replace & with & or &

    Look at the urlencode and urldecode functions.
    I have a name in the database: S&M
    when im trying to use $_GET to get it from the url for some reason it get me only "S"
    tried with and without decode
    Owner of uReviews & MovieDB - Movie Database

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by theunreal View Post
    I have a name in the database: S&M
    when im trying to use $_GET to get it from the url for some reason it get me only "S"
    tried with and without decode
    Why did you not bother to take his advice and use the two functions: urlencode() and urldecode()? Had you have done that, you'd have found the solution to your problem.

    Here's a quick demonstration of their usage:
    PHP Code:
    <?php

    $name 
    'S&M';

    $nameEncoded urlencode($name);

    echo 
    '<a href="get.php?name='$nameEncoded'">Click Me</a>';

    var_dump(urldecode($_GET['name']));

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    82
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tpunt View Post
    Why did you not bother to take his advice and use the two functions: urlencode() and urldecode()? Had you have done that, you'd have found the solution to your problem.

    Here's a quick demonstration of their usage:
    PHP Code:
    <?php

    $name 
    'S&M';

    $nameEncoded urlencode($name);

    echo 
    '<a href="get.php?name='$nameEncoded'">Click Me</a>';

    var_dump(urldecode($_GET['name']));
    it makes the url fine but my problem is
    when im trying to access the $_GET variable in the encoded/decoded url it returns only "S".... I have no idea why
    Owner of uReviews & MovieDB - Movie Database

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    When you urlencode the value in the URL then you will get the correct value in $_GET - there no other possibility! If you have doubts then please post your URL here so we can see if it's been poperly encoded.

    BTW, tpunt made a small mistake in his code:
    Code:
    var_dump(urldecode($_GET['name']));
    You don't use urldecode on $_GET variables because they are already decoded by the server! Just access $_GET['name'] directly and that's it. Only urlencode is needed here for encoding the URL.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    82
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    When you urlencode the value in the URL then you will get the correct value in $_GET - there no other possibility! If you have doubts then please post your URL here so we can see if it's been poperly encoded.

    BTW, tpunt made a small mistake in his code:
    Code:
    var_dump(urldecode($_GET['name']));
    You don't use urldecode on $_GET variables because they are already decoded by the server! Just access $_GET['name'] directly and that's it. Only urlencode is needed here for encoding the URL.
    I said I was trying to print the $_GET with and without the encode, both returns me "S", i have no idea why..
    all other gets without the "&" works
    Owner of uReviews & MovieDB - Movie Database

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    How you print GET is not the main problem. The problem is how you encode the URL. Please post the URL.

  9. #9
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    In your example
    Code:
    mysite.com/hocky&tocky
    the name "hocky&tocky" is a folder name, not a GET request.
    You have to create a valid link to access the GET variable, unless you have rewrite options.

    So, as you can read above, you need a link like this:

    1. http://mysite.com?hocky%26tocky // in case you have a special rewrite rule
    OR
    2. http://mysite.com?name=hocky%26tocky // in case you use classic GET

    if you access this link, the server will know about:
    1. $_GET['Whatever_Rewrite_Var_You_Have']
    OR
    2. $_GET['name']

    In my example, %26 is the same thing with urlencode('&') so, to create the link you will need, as tpunt wrote, something like this
    Code:
     <a href="get.php?name=<?php echo urlencode('hocky&tocky') ?>">Click Me</a>
    urlencode is used to create every link query.
    Check the manual for details.
    Be nice to nerds. Chances are you'll end up working for one - Bill Gates
    > photos | admin panel

  10. #10
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Please be aware if url rewrite is being used (Apache server) then mysite.com/hocky&stocky may be automatically transcoded to mysite.com/index.php?name=hocky&stocky

    If the OP is using url rewrite then (s)he needs to check that also.
    Ian Anderson
    www.siteguru.co.uk

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    BTW, tpunt made a small mistake in his code:
    Code:
    var_dump(urldecode($_GET['name']));
    You don't use urldecode on $_GET variables because they are already decoded by the server! Just access $_GET['name'] directly and that's it. Only urlencode is needed here for encoding the URL.
    Thanks for the information, Lemon Juice. I was not aware of that.

  12. #12
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    But it's important to note that auto-decoding by the server only applies to GET variables. If you want to get the value directly from $_SERVER['REQUEST_URI'] then the URL is not auto-decoded, we get the literal string from the browser address bar so in such cases both urlencode and urldecode are needed.

    If URL rewriting is on then it may still be another case depending on how the URL is rewritten. If the OP uses URL's like mysite.com/hocky&tocky then most probably he must be using some form of mod_rewrite. In such a case we would need to see the .htaccess file with the rules to see where the problem is.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    82
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    But it's important to note that auto-decoding by the server only applies to GET variables. If you want to get the value directly from $_SERVER['REQUEST_URI'] then the URL is not auto-decoded, we get the literal string from the browser address bar so in such cases both urlencode and urldecode are needed.

    If URL rewriting is on then it may still be another case depending on how the URL is rewritten. If the OP uses URL's like mysite.com/hocky&tocky then most probably he must be using some form of mod_rewrite. In such a case we would need to see the .htaccess file with the rules to see where the problem is.
    this is the .htaccess part of the url

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule hear/(.+)$ index.php?hear&music=$1 [L]
    Owner of uReviews & MovieDB - Movie Database

  14. #14
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    So now I can see where your problem is. If you have a URL like example.com/hear/hocky&stocky it becomes translated by your rule to:
    Code:
    example.com/index.php?hear&music=hocky&stocky
    Can you see now what is going on? The & sign is the delimiter of GET variables so in effect you receive these 3 GET variables in PHP:

    1. hear (empty value)
    2. music=hocky
    3. stocky (empty value)

    The solution is to instruct mod_rewrite to encode your variable before it gets to PHP. This can be achieved by using the B flag that is available since Apache 2.2:

    Code:
    RewriteRule hear/(.+)$ index.php?hear&music=$1 [L,B]
    You may notice that your URL's with special characters work without encoding. Technically such URL's are incorrect but most browsers will do the encoding for you, hence they will still work but for maximum compatibility it is good practice to always use urlencode when creating links like shown in earlier posts in this thread.

  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    82
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    So now I can see where your problem is. If you have a URL like example.com/hear/hocky&stocky it becomes translated by your rule to:
    Code:
    example.com/index.php?hear&music=hocky&stocky
    Can you see now what is going on? The & sign is the delimiter of GET variables so in effect you receive these 3 GET variables in PHP:

    1. hear (empty value)
    2. music=hocky
    3. stocky (empty value)

    The solution is to instruct mod_rewrite to encode your variable before it gets to PHP. This can be achieved by using the B flag that is available since Apache 2.2:

    Code:
    RewriteRule hear/(.+)$ index.php?hear&music=$1 [L,B]
    You may notice that your URL's with special characters work without encoding. Technically such URL's are incorrect but most browsers will do the encoding for you, hence they will still work but for maximum compatibility it is good practice to always use urlencode when creating links like shown in earlier posts in this thread.
    Awesome! it works, thanks.
    Owner of uReviews & MovieDB - Movie Database


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •