donglilian0061 2014-01-25 17:56
浏览 98
已采纳

PHP在URL中显示%20的空格?

this is a bit odd question and i never faced this before but here we are:

I have created a simple PHP/MYSQL shop. everything works etc

BUT when I add a product and there is a space OR / OR - in the category name and when i click on the category name in the front end it doesn't show the product(s) that are in that category and when i look at the URL in the address bar it will show it like this:

category_list.php?category=tooth-white%20range 

as you can see it has converted the tooth-white range to tooth-white%20range

if i write the category name like toothwhiterange it will work as it should!!

Here is how I get the categories to show up:

<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "config/connect.php"; 
$dynamicList2 = "";
$sql = "SELECT DISTINCT category FROM products";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $category = $row["category"];
             $dynamicList2 .= '<li>
       <a href="category_list.php?category=' . $category . '">' . $category . '</a>
      </li>';
    }   
} else {
    $dynamicList2 = "NO Cats Yet!";
}
?>

could someone please help me with this?

Thanks

EDIT:

sorry guys I really don't know how to explain this but here we go again:

the problem is when i put a space in the name of the category like name of the category, the name of the category will show up on the front end but when i click on it there wont be any products under that category even though there are some products under that category in the database.

However when i remove the spaces or any special characters like - or / from the category name and write it like nameofthecategory it works just fine and it will show the products under that category!

SECOND EDIT:

Here is how i get/display the products under each category depending on what category is selected:

<?php 
if (isset($_GET['category'])) {
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "config/connect.php"; 

$category = preg_replace('~[^a-zA-Z0-9]+~', '', $_GET['category']);

$cList2 = "";
$sql = "SELECT * FROM products WHERE category='".$category."'  LIMIT 35" ;
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $category = $row["category"];
             $stock = $row["stock"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $cList2 .= '<table style="float:left; margin-top:10px;" width="25%" border="0" cellspacing="0" cellpadding="0">
      <tr>
    <td align="center"><a href="product.php?id=' . $id . '"><img  style="border:solid 1px #999;" src="inventory_images/' . $id . 'Image1.jpg" width="124" height="124" /></a></td>
  </tr>
  <tr>
    <td id="nameHolder" class="nameHolder" style="text-align:left; padding-left:5px;" rowspan="" height="34"><a href="product.php?id=' . $id . '">' . $product_name . '</a></td>
  </tr>
  <tr>
    <td style="text-align:left; padding-left:5px; color:#C33; font-size:17px;" height="30"><strong style="color:#999;">Price:</strong> £' . $price . '</td>
  </tr>
  <tr>
    <td style="text-align:left; padding-left:5px; color:#C33; font-size:17px;" height="30">' . $stock . '</td>
  </tr>
  <tr>
    <td style="text-align:left; padding-left:5px;" height="15"><a href="product.php?id=' . $id . '">view</a></td>
  </tr>
  <tr>
    <td height="50" align="left"><form method="post" action="product.php?id=' . $id . '"><input type="submit" class="button" name="button" id="button" value="Add to cart" /></form></td>
  </tr>
</table>';
    }
}
} else {
    $cList2 = "We have no products listed in our store yet";
}

?>
  • 写回答

3条回答 默认 最新

  • dounie5475 2014-01-25 18:05
    关注

    Its normal that %20 is added, as spaces are not really url friendly and abit discouraged within file naming conventions, the 20 is the hex(base16) representation of a space char.

    If it effect your code you could always use urldecode() to convert it back.

    Why does it have to be encoded?

    Going to the HTTP layer when you make a request to the web your browser will open a socket to the server and do a HTTP request:

    GET category_list.php?category=tooth-white%20range HTTP/1.1 //Good

    GET category_list.php?category=tooth-white range HTTP/1.1 //Bad

    If you have a space this will cause the request to break as the protocol there is a space after the url in the HTTP request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算