首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将部分值提取并重新插入到数据库中。

问将部分值提取并重新插入到数据库中。
EN

Stack Overflow用户
提问于 2013-05-07 00:32:49
回答 1查看 45关注 0票数 0

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

这是我的代码:

代码语言:javascript
复制
<?
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很好(因为我猜没有空格?)然而,大屠杀应该是大屠杀竞赛。

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

redir ()是一个自定义函数,它使用JS来redir,这样我就不必在headers中使用了。

EN

回答 1

Stack Overflow用户

发布于 2013-05-07 04:26:07

我通过在PHP urlencode函数中封装问题变量来解决这个问题。我从中得到的是,select标记在插入到数据库中时,很可能会将它们的值从空格中去掉(因此,在主空间之后的任何值),所以当我需要调用变量时,我必须将其封装在PHP urldecode函数中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16409399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档