0

The site I manage uses php urls to show a detail.php dynamically for each product (still new to php, please bear with me).

the currently look like this:

domain.com/detail.php?id=123

can I use a different attribute (not sure if that's what it's called) so I can have slightly more user friendly urls?

Like:

domain.com/detail.php?name=Product+Name

and if so... how? for the life of me I can't figure it out or search for the right terms (probably just don't know their correct names yet). I figured it would be simple as a %20 instead of the spaces, but as you all probably know, this doesn't do it.

I have name set to varchar, and I have no idea if that makes a difference.

any help is appreciated, this isn't an urgent thing.

6
  • Can you show the code that generates this URL?
    – Travesty3
    May 2, 2012 at 18:41
  • <a href="detail.php?id=<?php echo $row_featuredItems['id']; ?>"> just an example of a featured item list url May 2, 2012 at 18:50
  • And that doesn't work for you, just the way it is, leaving the space there just as a space and not encoding or decoding it?
    – Travesty3
    May 2, 2012 at 18:57
  • i wish i could say i completely understand your question. when I do it the same way replacing 'id' with 'name' and id= with name= all the spaces go to %20's, and the content is empty May 2, 2012 at 19:05
  • Take a look at my answer below.
    – Travesty3
    May 2, 2012 at 19:07

5 Answers 5

6

There's a few different methods of accomplishing what you want. Without digging straight into the PHP code to change the underlying logic you could have a look at enabling and configuring mod_rewrite within your Apache implementation. Here's a quick and simple mod_rewrite tutorial:

http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/

1
  • I've been combing throughout all the mod_rewrite tutorials out there, and I figured that'd be the next step. Wouldn't it be a simpler mod_rewrite rule if the url already had the product name in it? May 2, 2012 at 18:54
1

I'm really not sure where you're having trouble. This very basic example works just fine for me:

<?php
    if (isset($_GET["name"]))
        die("name = {$_GET["name"]}");    /* outputs "name = Product Name" */

    $row = "Product Name";
?><a href="test.php?name=<?php echo $row; ?>">Click here</a>
1
  • I can get the product name to output into the url fine, I think Marc B knows the problem, it's with my query on my detail page i think. May 2, 2012 at 19:14
1

Look at the functions to work with URLs in PHP: http://www.php.net/manual/en/ref.url.php.

0

You are probably looking for the PHP-method urlencode() to encode a string so that it can be passed as a querystring-variable. You can then user urldecode() to decode the querystring-variable.

2
  • so, would something like this work? <?php echo '<a href="detail.php?name=', urlencode($ $row_featuredItems['name']), '">'; ?> May 2, 2012 at 18:59
  • @paul_the_dino Yes, something like that. This would probably work <?php echo '<a href="detail.php?name=' . urlencode($row_featuredItems['name']) . '">'; ?> May 2, 2012 at 20:06
0

You're probably not building your query right, and that'd mean you're vulnerable to SQL injection attacks. Retrieving a string value from a query is no different than retrieving an integer, but how you use that string later on DOES make a huge difference:

$name = $_GET['name'];
$safe_name = mysql_real_escape_string($name);
$sql = "SELECT ... FROM yourtable WHERE name='$safe_name'";
$result = mysql_query($sql) or die($sql);

When using strings in queries, they MUST be enclosed with quotes. If they're not, they're treated as field names, table names, etc...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.