Extracting data from mysql database then placing it in URLS?

Discussion in 'PHP' started by scm22ri, May 28, 2012.

  1. #1
    Hi everyone!


    I figured out hot to pass data from my mysql database into my URLS!


    I just have one question relating to some of my code and info. in my database. In my database if a city is two words. For example, "Las Vegas" the current code below (which works for one worded cities) isn't working. What would I have to change in my script to allow for multiple worded cities? Thanks everyone!

    http://whatsmyowncarworth.com/auto/boston
    http://whatsmyowncarworth.com/auto/Las Vegas <<--- not echoing out information.
    http://whatsmyowncarworth.com/auto/providence
    http://whatsmyowncarworth.com/auto/miami

    [QUOTE]<?php
    
    
    ob_start(); // handle redirects
    
    
    ob_end_flush();
    
    
    include('init.php'); // connection to database
    
    
    if (isset($_GET['u'])) {
        
        $city = mysql_real_escape_string($_GET['u']); 
         // protection against mysql injection
        if (ctype_alnum($city)) {
            $data = mysql_query("SELECT State, City FROM cars WHERE City='$city'" );
            if (mysql_num_rows($data) > 0) {
                while ($row = mysql_fetch_assoc($data)) {
                    echo $row["City"];
                }
            }
        }
    }
            
    ?>
    [/QUOTE]
    PHP:

     
    scm22ri, May 28, 2012 IP
  2. kbduvall

    kbduvall Peon

    Messages:
    71
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    0
    #2
    Try something like this:
    
    $city = mysql_real_escape_string(urldecode($_GET['u']));
    
    PHP:
     
    kbduvall, May 28, 2012 IP
  3. scm22ri

    scm22ri Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    Hey Keith,

    Thanks for the reply but it's not working.

    http://whatsmyowncarworth.com/auto/Las%20Vegas

     
    scm22ri, May 29, 2012 IP
  4. kbduvall

    kbduvall Peon

    Messages:
    71
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    0
    #4
    Yeah, sorry about that. ctype_alpha consideres the space character as non-alphanumeric. And of course %20 from the encoded URL is also non-alphanumeric. So the issue is that the string with a space isn't passing your validation. This should work:
    
    <?php
    include('init.php'); // connection to database
    
    if (isset($_GET['u'])) {
    	$city = mysql_real_escape_string(urldecode($_GET['u']));
    	// protection against mysql injection
    	if (ctype_alnum(str_replace(' ', '', $city))) {
    		$data = mysql_query("SELECT State, City FROM cars WHERE City='$city'" );
    		if (mysql_num_rows($data) > 0) {
    			while ($row = mysql_fetch_assoc($data)) {
    				echo $row["City"];
    			}
    		}
    	}
    }
    ?>
    
    PHP:
    $city will still contain the city with the space, but ctype_alnum will see it without the space.
     
    kbduvall, May 29, 2012 IP
  5. burlesk

    burlesk Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Some advice:

    1. mysql_real_escape_string is enough to prevent SQL injections, the ctype_alnum is excessive;
    2. it's better to use MySQL PDO instead of mysql_query php.net/manual/en/ref.pdo-mysql.php
    3. there's no need to check for mysql_num_rows, you can just run while right after the query.
     
    burlesk, May 29, 2012 IP
  6. kbduvall

    kbduvall Peon

    Messages:
    71
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    0
    #6
    MySQLi is also a better approach to preventing SQL injections. Hell, anything other than the standard mysql functions is better.

    If you're using ctype strictly for SQL injection prevention, yeah it may be unnecessary .. but it is also my opinion that all external data should be validated/filtered. If I know that my "city" column should only contain alphanumeric characters, with maybe some spaces, then there is no reason why I shouldn't verify that the user input is of that format. I'd rather be able to gracefully provide the user with some error/alert than just return an empty result with no explanation.
     
    kbduvall, May 29, 2012 IP
  7. scm22ri

    scm22ri Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #7
    Hi pmkbduvall and everyone else!

    Thanks for helping with out with the code. I appreciate that! It's working great!

    The only thing I'm trying to figure out now is the space in the URL. How would I get append a - to two lettered words. For example, las-vegas instead of Las%20Vegas. I'm assuming I would need to append a - in the cars.php syntax and not the .htaccess code correct?

    Thanks everyone!
     
    scm22ri, May 29, 2012 IP
  8. kbduvall

    kbduvall Peon

    Messages:
    71
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    0
    #8
    kbduvall, May 29, 2012 IP
  9. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #9
    Exactly like Keith suggests... You should convert spaced to dashes for URLs. Then Convert dashes to Spaces before executing your SQL statement.
     
    NetStar, May 29, 2012 IP
  10. burlesk

    burlesk Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Don't forget, that some city names may contain hyphens (Sedro-Woolley, WA), apostrophes (O'Fallon, IL) and maybe other special characters.
     
    burlesk, May 30, 2012 IP