【问题标题】:Pulling and reinserting to database removes a part of the values拉取并重新插入数据库会删除部分值
【发布时间】:2013-05-07 00:32:49
【问题描述】:

我正在从数据库中提取信息并将其放入选择字段中。在选择字段中,它看起来是完整的,但在重新插入数据库时​​,初始空格之后的任何单词都会被剥离。

这是我的代码:

<?
require_once('./inc/glob_head.php');
if (isset($_POST['submitNewOther']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        $checkDB = $database->prepare('SELECT * FROM mainSite_others WHERE forGame=:fg AND otherType=:ot AND otherName=:otn');
        $addToDB = $database->prepare('INSERT INTO mainSite_others(forGame,otherType,otherName,otherDesc,otherCost,membeOnly,otherPageContent) VALUES (:fg,:ot,:otn,:od,:oc,:mo,:opc)');
    } catch (PDOException $e) {
        error_log($e->getMessage());
        exit();
    }
    $checkDB->execute(array(':fg'=>$_POST['forGame'],':ot'=>$_POST['otherType'],':otn'=>$_POST['otherName']));
    if ($checkDB->rowCount() == 0) {
        try {
            $addToDB->execute(array(':fg'=>$_POST['forGame'],':ot'=>$_POST['otherType'],':otn'=>$_POST['otherName'],':od'=>$_POST['otherDesc'],':oc'=>$_POST['otherCost'],':mo'=>$_POST['membeOnly'],':opc'=>$_POST['otherPageContent']));
            redir('addOther.php?action=success');
        } catch (PDOException $e) {
            error_log($e->getMessage());
            exit();
        }
    } else {
        echo "Other already in database";
    }
} elseif (isset($_GET) && $_GET['action'] == 'success') {
?>
    <h2>"Other" added to database.</h2>
    <h4><a href='addOther.php'>Add another</a> or choose something new to do.</h4>
<?
} else {
?>
                        <fieldset>
                            <legend><h2>Add other</h2></legend>
                            <form name='addOther' id='addOther' method='POST'>
                                <select name='forGame'>
                                    <?
                                    try {
                                        $listOfGames = $database->prepare('SELECT * FROM mainSite_games');
                                        $listOfGames->execute();
                                        $games = $listOfGames->fetchAll(PDO::FETCH_ASSOC);
                                        foreach ($games as $game) {
                                            print '<option value='.$game['gameName'].'>'.$game['gameName'].'</option>';
                                        }
                                    } catch (PDOException $e) {
                                        error_log($e->getMessage());
                                        exit();
                                    }
                                    ?>
                                </select><br />
                                <select name='otherType'>
                                    <option value='Item'>Item</option>
                                    <option value='Place'>Place</option>
                                    <option value='Character'>Character</option>
                                </select><br />
                                <input type='text' name='otherName' placeholder='Other name' required /><br />
                                <input type='text' name='otherDesc' placeholder='Other examine (or NA)' required /><br />
                                <input type='text' name='otherCost' placeholder='Other cost (price/NA)' required /><br />
                                <input type='text' name='membeOnly' placeholder='Members Only (Yes/No)' required /><br />
                                <textarea name="otherPageContent" class="span12" rows="10" placeholder="Other page content" required></textarea><br />
                                <input type='submit' name='submitNewOther' class='btn btn-primary' /><br />
                            </form>
                        </fieldset>
                    </div>
                </div>
            </div>
        </body>
    </html>
<?
}
$database = null;
?>

底部(纯 html 部分)是它从数据库中提取出来的地方——第一个选择是它被放置的地方。观看时是完整的:

现在,当提交此表单时,它成功进入数据库,但显示为 - 因此破坏了我的网站(现在允许用户查看此游戏内容,因为游戏的数据库/任务不存在它知道(它正在检查整个名称)。这就是它在 SQL 管理中的样子:

RuneScape 很好(因为我猜没有空格?)但是 Carnage 应该是 Carnage Racing。

我的代码是否存在导致此问题的问题?这不是它发生的唯一页面,但我想如果我找到源,我可以在所有页面上修复它。

redir()是一个自定义函数,使用JS进行redir,这样我就不用玩headers了

【问题讨论】:

  • 已解决 - 通过使用 urlencode 修复。
  • 最好发布您自己问题的答案并附上解释,以便其他人可以从中受益。

标签: php sql pdo insert


【解决方案1】:

我通过将问题变量封装在 PHP urlencode 函数中解决了这个问题。我从中获取的是,当插入数据库中时,很可能会剥离它们的值(因此是在主要空间之后的任何内容),因此当我需要调用变量时,我将不得不封闭它在 PHP urldecode 函数中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多