Jump to content

Can't account for spaces in my database when echoing out data?


mcc_22ri

Recommended Posts

Hi Everyone,

 

What I'm trying to do is echo out information from a mysql database into my URL. I have done that (please see below code) but what I haven't figured out yet is for the code to handle spaces or two words. For example, if you visit the below URLs the page will echo out the city name (which is what I want) but I have a city called "Las Vegas" in my mysql database which is obviously two words and when I type in las vegas, las-vegas or Las Vegas etc ... at the end of the URL the code isn't echoing out anything. I also just recently added a urldecode within my code to see if that works but it appears it isn't. What am I doing wrong? What do I have to change in my syntax? Thanks everyone!

 

http://whatsmyowncarworth.com/auto/miami

http://whatsmyowncarworth.com/auto/providence

http://whatsmyowncarworth.com/auto/albany

http://whatsmyowncarworth.com/auto/boston

http://whatsmyowncarworth.com/auto/las-vegas <--- not echoing city name

http://whatsmyowncarworth.com/auto/Las%20Vegas <--- not echoing city name

 

<?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($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"];
		}
	}
}
}

?>

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

.htaccess

 

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /auto/cars.php?u=$1 [NC]

http://whatsmyowncarworth.com/auto/las-vegas <--- not echoing city name

http://whatsmyowncarworth.com/auto/Las%20Vegas <--- not echoing city name

 

For the first one, you'd want to strreplace '-' with ' '

For the second, when you do the urldecode, make sure your comparison is case insensitive.

Hey Josh,

 

Thanks for the reply. My mistake, I wasn't quite sure what you meant. Below are a few examples of what my current php syntax outputs. Everything currently works besides "Las Vegas". What do you think?

 

http://whatsmyowncarworth.com/auto/miami

http://whatsmyowncarworth.com/auto/providence

http://whatsmyowncarworth.com/auto/albany

http://whatsmyowncarworth.com/auto/boston

http://whatsmyowncarworth.com/auto/las-vegas <--- not echoing city name

http://whatsmyowncarworth.com/auto/Las%20Vegas <--- not echoing city name

where are you echoing it?  are you echoing it inside this condition: if (ctype_alnum($city)) { ?  Did you see my post about that condition?

 

oh and another thing I noticed... you wrap your query in a if (ctype_alnum($city)) {...} condition.  Well spaces and hyphens will cause that condition to fail, no query made.

Brillant!

 

It's now working!

 

I just have to work on making the URL prettier. I'm assuming I would have to edit my .htaccess to fix the spacing issue?

 

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

 

<?php

include('init.php'); // connection to database

if (isset($_GET['u'])) {

$city = mysql_real_escape_string(urldecode($_GET['u']));
	// protection against mysql injection
if ($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 $_GET['u'];
		}
	}
}
}

?>

Hi jesirose and everyone!

 

I appreciate the reply. I changed things up a little bit and the code seems is working. The below code isn't very secure so I figured out a way to make it so (or so I think. Look at the code in this forum post). I'm still trying to figure out how to add a - in between city names. For example, instead of Las%20Vegas las-vegas would be better but I'm a little confused on how to do that? Any pointers anyone?

 

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

 

<?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"];
            }
        }
    }
}
?>

The way I understand it, you have in your database "las vegas" and you want to be able to put into the address bar ../las-vegas and treat the city as "las vegas" right?  Assuming your mod rewrite is working correctly and it is putting it into that u url param...this will allow you to do ../las-vegas or ../las%20vegas

 

<?php
include('init.php'); // connection to database

// if city exists...
if (isset($_GET['u'])) {
    // decode and replace hyphen with space
    $city = str_replace('-',' ',urldecode($_GET['u']));
    
    // if value contains only letters, numbers or spaces...
    if ( preg_match('~^[a-z0-9 ]+$~i',$city) ) {
        // select data from database...
        $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"];
            }
        }
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.