mcfmullen Posted December 5, 2010 Share Posted December 5, 2010 I have the following code that outputs a url. It searches my MySQL database for the name of a given item within a theme: $relative = 'http://www.domain.com/wp-content/'; $url1= $relative . 'themes/arjuna-x/itemspec.php?type='; $url2='&item='; <a class="entry-thumbnails-link" href="<?php echo $url1.'Themes'.$url2.$rowThemes['Theme']; ?>"> The output is as follows: http://www.domain.com/wp-content/themes/arjuna-x/itemspec.php?type=Themes&item=(insert variable here) All is good up to this point. My problem however, is that some items (in this case: theme names) have apostrophes in them. For example: April Fool's. This results in breaking my php code. How can I get php to call these variables with apostrophes so that they don't break my php? Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/ Share on other sites More sharing options...
trq Posted December 5, 2010 Share Posted December 5, 2010 url_encode. Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1143368 Share on other sites More sharing options...
mcfmullen Posted December 5, 2010 Author Share Posted December 5, 2010 My database isn't finding the item after retrieving it: <a href="<?php echo $url1.'Themes'.$url2.urlencode($rowThemes['Theme']); ?>"> The url appears as: http://www.domain.com/wp-content/themes/arjuna-x/itemspec.php?type=Themes&item=April+Fool%27s My itemspec.php code: <?php include("../../../wp-blog-header.php"); ?> <?php if (isset($_GET['item'])) { include("variables.php"); switch (trim($table)) { case 'Mystery': $Name = 'Mystery'; break; case 'Themes': $Name = 'Theme'; break; default: $Name = 'Name'; break; } switch (trim($table)) { case 'Themes': $sql = "SELECT * FROM " . $table . " WHERE ". $Name . " = '" . mysql_real_escape_string($item) . "'"; break; default: $sql = "SELECT * FROM " . $table . " LEFT JOIN " . $table . "Themes USING ($Name) LEFT JOIN " . $table . "Methods USING ($Name) WHERE " . $table . "." . $Name . " = '" . mysql_real_escape_string($item) . "'"; break; } $answer = mysql_query($sql); if ($answer) { if (mysql_num_rows($answer) > 0) { $row = mysql_fetch_assoc($answer); ?> BUNCH OF SITE STUFF <?php } else { echo "No $table found by that name."; } } else { echo "No type found by that name."; } } else { echo "No type or item selected."; } ?> The error I'm getting is: No Themes found by that name. The code is working for those themes without postrophes. Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1143416 Share on other sites More sharing options...
trq Posted December 6, 2010 Share Posted December 6, 2010 You will need to use url_decode on the processing end. Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1143423 Share on other sites More sharing options...
mcfmullen Posted December 6, 2010 Author Share Posted December 6, 2010 I've tried using that to no avail. I get the same error. I'm using a $_GET which if I understand correctly, automatically decodes anyway... Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1143759 Share on other sites More sharing options...
trq Posted December 7, 2010 Share Posted December 7, 2010 Can you echo your query to see what the database is actually getting? Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1143766 Share on other sites More sharing options...
hennzo Posted December 7, 2010 Share Posted December 7, 2010 Hi, instead of urlencode(), use rawurlencode() Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1143793 Share on other sites More sharing options...
mcfmullen Posted December 12, 2010 Author Share Posted December 12, 2010 Using rawurlencode and rawurldecode results in this being stored in the variable: Children\'s Hospital Using urlencode and urldecode results in this being stored in the variable: UCSF Benioff Children\'s Hospital So both methods result in the same incorrect output. Is there any way to rectify this? Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1146188 Share on other sites More sharing options...
BlueSkyIS Posted December 12, 2010 Share Posted December 12, 2010 I suspect the apostrophes are in your data. If so, you'll need to either fix your data (best idea), or use strip_slashes() on the values to remove the slash in front of special characters. Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1146225 Share on other sites More sharing options...
mcfmullen Posted December 12, 2010 Author Share Posted December 12, 2010 I don't see what the logic is in "fixing" my data. Some thing simply need apostrophes. Why are slashes inserted to begin with anyway? Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1146234 Share on other sites More sharing options...
BlueSkyIS Posted December 12, 2010 Share Posted December 12, 2010 IF the slashes are in your database, there is no way for us to know how they got there unless you explain how the data is entered. Are the slashes in your data? IF there are slashes in your data, you probably don't want them there, so fixing your data (and fixing whatever is adding slashes) seems logical to me. The alternative is to leave everything malfunctioning and write additional code to get around the problem. always. Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1146248 Share on other sites More sharing options...
mcfmullen Posted December 12, 2010 Author Share Posted December 12, 2010 Ah, I thought you were talking about the apostrophes and not the slashes. The slashes are defininately not in the database. I have no clue what is causing the slashes to appear. The url is called using this: <a class="entry-thumbnails-link" href="<?php echo $url1.'Themes'.$url2.urlencode($rowThemes['Theme']); ?>"> Where: $relative = 'http://www.domain.com/wp-content/'; $url1= $relative . 'themes/itemspec.php?type='; $url2='&item='; with $rowThemes['Theme'] containing the Theme name (in this case: Children's Hosiptal). The url results in this: http://localhost/wp-content/themes/itemspec.php?type=Themes&item=Children%27s%20Hospital the item is then called using this: $item = stripslashes(urldecode($_GET['item'])); Obviously, I would prefer it if I did not have to use stripslashes. Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1146257 Share on other sites More sharing options...
BlueSkyIS Posted December 12, 2010 Share Posted December 12, 2010 Okay, it must be the server adding slashes to GET variables due to magic_quotes being turned on. You'll need to either continue using stripslashes() or turn off magic_quotes via php.ini. Or account for both possibilities by checking for magic_quotes before using stripslashes(): if (get_magic_quotes_gpc()) { $item = stripslashes($item); } Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1146265 Share on other sites More sharing options...
mcfmullen Posted December 12, 2010 Author Share Posted December 12, 2010 Thank you so much! Link to comment https://forums.phpfreaks.com/topic/220763-php-passing-variables-to-url/#findComment-1146269 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.