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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题