URL编码>>>构造

时间:2013-07-29 15:12:41

标签: php mysql

我在设置一个变量到我的echo<<<< _END construct

中的链接时遇到了问题

这是我的代码:

<?php //submit_build.php

include_once 'header.php';
require_once 'login_builds.php';
include_once 'functions.php';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

mysql_select_db($db_database)
    or die("Unable to select database: " . mysql_error());

?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Weapon Build Creator</title>

<link href="styles/main.css" rel="stylesheet" type="text/css" />

<style>

.bannerdiv
{
    display: block;
    width: 620px;
    height;
    position: static;
    background-image:url('images/news_banner_072813.jpg');
    text-align: center; 
    border-radius: 15px;
    moz-border-radius: 15px;
    moz-box-shadow: 0 0 30px 5px #999;
    box-shadow: 0 0 30px 5px #999;
    height: 300px;
    margin-left: 25px;

}
.form
{
    display: block;
    width: 670px;
    left: 50%;
    margin-left: -335px;
    margin-top: 100px;
    float: left;
    position: absolute;
    background-image:url('images/greystripe_bg.jpg');
    text-align: center; 
    border-radius: 15px;
    moz-border-radius: 15px;
    moz-box-shadow: 0 0 30px 5px #999;
    box-shadow: 0 0 30px 5px #999;
    height: 1000px;
}


</style>

</head>

<body style="background-image: url('images/bg.jpg')">

<div id="form" class="form">
    <div class="newsdiv"><p class="title">Search / Browse Builds</p>
        <p class="newsbody">&nbsp;Build your search query by inputting at least 1 of 
        the fields below.</p>
    <form method="post" action="search_query.php"><p class="timestamp">
        <input name="buildname" type="text" style="vertical-align:middle">&nbsp;&nbsp; Build Name</p>
        <p class="timestamp">
        <select name="weapon" class="dropdown" style="vertical-align:middle">
        <option></option>
        <option>Acrid</option>
        <option>Afuris</option>
        <option>Akbolto</option>
        <option>Aklato</option>
        <option>Amphis</option>
        <option>Ankyros</option>
        <option>Bo</option>
        <option>Boar</option>
        <option>Bolto</option>
        <option>Boltor</option>
        <option>Braton</option>
        <option>Braton Prime</option>
        <option>Braton Vandal </option>
        <option>Bronco</option>
        <option>Bronco Prime</option>
        <option>Burston </option>
        <option>Ceramic Dagger</option>
        <option>Cronus</option>
        <option>Dark Dagger</option>
        <option>Dark Sword</option>
        <option>Dera</option>
        <option>Despair</option>
        <option>Dread</option>
        <option>Dual Broncos</option>
        <option>Dual Cleavers</option>
        <option>Dual Ether</option>
        <option>Dual Heat Swords</option>
        <option>Dual Skana</option>
        <option>Dual Zoren</option>
        <option>Ether Daggers</option>
        <option>Ether Sword</option>
        <option>Fang</option>
        <option>Fang Prime</option>
        <option>Flux Rifle</option>
        <option>Fragor</option>
        <option>Furax</option>
        <option>Furis</option>
        <option>Glaive</option>
        <option>Gorgon </option>
        <option>Grakata</option>
        <option>Gram</option>
        <option>Hate</option>
        <option>Heat Dagger</option>
        <option>Heat Sword</option>
        <option>Hek</option>
        <option>Hikou</option>
        <option>Ignis</option>
        <option>Jaw Sword</option>
        <option>Kestrel</option>
        <option>Kogake</option>
        <option>Kraken</option>
        <option>Kunai</option>
        <option>Lanka</option>
        <option>Lato</option>
        <option>Lato Prime</option>
        <option>Lato Vandal</option>
        <option>Latron</option>
        <option>Latron Prime</option>
        <option>Lex</option>
        <option>Machete</option>
        <option>Mire</option>
        <option>MK1-Braton</option>
        <option>Ogris</option>
        <option>Orthos</option>
        <option>Orthos Prime</option>
        <option>Pangolin Sword</option>
        <option>Paris</option>
        <option>Paris Prime</option>
        <option>Plasma Sword</option>
        <option>Prova</option>
        <option>Reaper Prime</option>
        <option>Scindo</option>
        <option>Seer</option>
        <option>Sicarus</option>
        <option>Skana</option>
        <option>Skana Prime</option>
        <option>Snipetron</option>
        <option>Snipetron Vandal</option>
        <option>Sobek</option>
        <option>Spectra</option>
        <option>Strun</option>
        <option>Supra</option>
        <option>Torid</option>
        <option>Twin Vipers</option>
        <option>Vasto</option>
        <option>Viper</option>
        <option>Vulkar</option>
        </select>&nbsp;&nbsp; Weapon</p>
        <p class="timestamp">
        <select class ="dropdown" name="category" style="vertical-align:middle">
        <option></option>
        <option>Defensive</option>
        <option>Balanced</option>
        <option>High DPS</option>
        </select>&nbsp;&nbsp; Category</p>
        <p class="timestamp">
        <input name="id" type="text" style="vertical-align:middle">&nbsp;&nbsp; 
        Weapon ID Number</p>
        <p class="timestamp">
        <input name="search" type="submit" value="Search Builds">&nbsp;</p>
        <p class="timestamp">



        <p class="title">SEARCH RESULTS</p>

<?php //search result table

if (isset($_POST['buildname']) ||
    isset($_POST['weapon']) ||
    isset($_POST['category']) ||
    isset($_POST['id']))

{

$sqlArray = array();

    if(!empty($_POST['buildname'])){
        $sqlArray[] = "buildname='" . mysql_real_escape_string($_POST['buildname']) . "'";
    }

    if(!empty($_POST['weapon'])){
        $sqlArray[] = "weapon='" . mysql_real_escape_string($_POST['weapon']) . "'";
    }

    if(!empty($_POST['category'])){
        $sqlArray[] = "category='" . mysql_real_escape_string($_POST['category']) . "'";
    }

    if(!empty($_POST['id'])){
        $sqlArray[] = "id='" . mysql_real_escape_string($_POST['id']) . "'";
    }

    $searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons " .
                    "WHERE " . implode(' AND ', $sqlArray);

$result = mysql_query($searchstring);

if (!$result) die ("Database access failed: " . mysql_error());

$rows = mysql_num_rows($result);

echo<<<_END

        <table class="searchresult">
        <tr style="width:100%">
        <td class="newsbody" style="width:5%">ID</td>
        <td class="newsbody" style="width:40%">Build Name</td>
        <td class="newsbody" style="width:20%">Weapon</td>
        <td class="newsbody" style="width:20%">Category</td>
        <td class="newsbody" style="width:15%">Author</td>
        </tr>
_END;

for ($j = 0; $j < $rows; ++$j)
{
$row = mysql_fetch_row($result);
echo<<<_END

        <tr style="width:100%">
        <td class="newsbody" style="width:5%">$row[3]</td>
        <td class="newsbody" style="width:40%"><a href="view_build.php?buildname=$row[5]">$row[0]</a></td>
        <td class="newsbody" style="width:20%">$row[1]</td>
        <td class="newsbody" style="width:20%">$row[2]</td>
        <td class="newsbody" style="width:15%">$row[4]</td>
        </tr>

_END;
}

echo<<<_END

        </table>

_END;

}

?>

</form>
<p><br /><br /><br /><br /><br /></p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p><br /></p>
    </div>

</body>
</html>

我遇到问题的代码行是第236行,在代码底部附近加粗。

这一个:

<td class="newsbody" style="width:40%"><a href="view_build.php?buildname=$row[5]">$row[0]</a></td>

我基本上希望搜索结果显示一个值,例如“Dual Broncos”,然后在文本周围有一个链接到view_build.php的超链接?buildurl = Dual + Broncos。

我不知道如何正确插入urlencode函数,因为当我放置标记时,它表示我的HTML是错误的(它表示表格列结束标记无效/不必要)。

所以我试着让urlencode在数据库输入时工作,这样每个对象都有一个“buildname”和一个“buildurl”,其中buildurl = urlencode($ buildname)。此代码有效,我的数据库输入了值“Dual + Broncos”,但是当我检索行将其放入view_build.php?buildurl = $ row [5]位置的链接时,它不会显示“+”应该在编码的URL中。

对此有何帮助?或者建议如何以另一种方式执行此功能?

非常感谢!

PS。我包含了我的整个代码,因此您可以看到HTML标记和PHP标记的开始和结束位置。

2 个答案:

答案 0 :(得分:0)

只需使用变量将编码值存储在之后插入的heredoc之前。

$value = urlencode($row[5]);
...
<td class="newsbody" style="width:40%"><a href="view_build.php?buildname=$value">$row[0]</a></td>

答案 1 :(得分:0)

只需在循环外进行urlencoding:

for($j = 0; ...) {
   $encoded = urlencode($row[0]);
   echo <<<EOL

....?buildname=$encoded
EOL;
}

你可以使用变量函数和“内联”编码,例如

$enc = 'urlencode';
echo <<<EOL
 ... ?buildname={$enc($row[0])}
EOL;

但是这样的代码很快变得完全无法管理并且很难调试。