简体   繁体   中英

PHP adding leading space, tried trim, 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.

Change :

VALUES(\" $gameName \" 

to :

VALUES(\"$gameName\"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM