douhuan2101 2013-04-28 06:33
浏览 56
已采纳

PHP添加领先空间,尝试修剪,ltrim,str_replace

I'm trying to take input, gameName, which is very sensitive to leading spaces as it needs to work with other functions.

When I enter it into the input field, there is no space. When I check it in the DB, directly after being processed, there is a space in front of it. When I pull it from the DB, there is no space, and reinserting it to the DB (after being pulled) removes the space.

The offending code for the page is:

if (isset($_GET) && $_GET['page'] == 'addGame') {
    if (isset($_GET) && $_GET['page'] == 'addGame' && $_GET['action'] == 'success') {
?>
      <h2>Game added to database.</h2>
      <h4><a href='index.php?page=addGame'>Add another</a> or choose something new to do.</h4>
<?
    } else {
      include('./inc/addGameProc.php');
?>
      <fieldset>
      <legend><h2>Add game</h2></legend>

      <form name='addGame' id='addGame' method='POST'>
        <input type='text' name='gameName' placeholder='Game name' required /><br />
        <textarea name="gameDesc" class="span12" rows="10" placeholder='Game description' required></textarea><br />
        <input type='submit' name='submitNewGame' class='btn btn-primary' /><br />
      </form>
      </fieldset>
<?

    }
}

The add game proc you see is:

    <?
    if (isset($_POST['submitNewGame']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
        $database = new database('localhost','wwwander_sradmin','editedout','wwwander_srmain','mysqli');
        $database->openConnection();

            //save and escape entered material
            $gameName = ltrim($_POST['gameName']);
            $gameDesc = $database->escapeString($_POST['gameDesc']);

            //check that material not already in db
            $checkDB = $database->queryDB("SELECT * FROM mainSite_games WHERE gameName='$gameName' AND gameDesc='$gameDesc'");
            if ($database->dbNumRows($checkDB) == 1) {
                echo "Game already in database";
            } else {
                //add entered material into db
                $addToDB = $database->queryDB("INSERT INTO mainSite_games(gameName, gameDesc) VALUES(\" $gameName \", \" $gameDesc \")");
                if (!$addToDB) {
                    echo "An error occured.";
                } else {
                    header('Location: index.php?page=addGame&action=success');
                }
            }

        $database->closeConnection();
    }
?>

You can see my tried and failed ltrim there.

Of course, SO is my last try here. I've done all I know. Any ideas? Any more resources I can provide that I've left out?

EDIT--

After being pulled, there IS a space. The reason I said there wasn't is because where I usually pull it, it's going into a select, where I suppose spaces don't matter since they aren't taken into account? I dunno.

  • 写回答

1条回答 默认 最新

  • douxie7738 2013-04-28 06:47
    关注

    Change :

    VALUES(\" $gameName \" 
    

    to :

    VALUES(\"$gameName\"
    

    I think the space you're seeing is the one directly after the quotes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?