1

I'm reading values from a table and displaying them in a form that can be changed updated by the user. The problem is every time I make an update a bunch of spaces are being added to each value in the table.

Here is the basic code:

function ReadHTML($str) {
    return htmlspecialchars(stripslashes($str), ENT_QUOTES);
}

$query = "SELECT * FROM " . $table_name . " WHERE id = " . $row_id;

$result = mysqli_query ($connection, $query ) or die ("Database query failed: " . mysqli_error($connection));

$row = mysqli_fetch_assoc($result);

$q1 = ReadHTML($row['q1']);

When I display the values in the form it looks fine. But it's when I update the values that the spaces get added.

When I process the updates it looks something like this:

function mysqli_prep(&$connection, $value) {
    $detagged = strip_tags($value);
    $escaped = mysqli_real_escape_string($connection, $detagged);
    return $escaped;
}

$q1 = mysqli_prep($connection, $_POST['q1'])

$query = "UPDATE my_form SET  
        q1='$q1'";

Can anyone see what I'm doing wrong?


HTML looks like this:

<textarea class="textarea form_textarea_field" name="comment_1" placeholder="Comments">
                <?php if($saved_form == true) {echo $q1;} ?>
            </textarea>

When I look at the DOM in my browser it looks like this:

<textarea class="textarea form_textarea_field" name="comment_1" placeholder="Comments">
                " Test text "
            </textarea>

But I never added those quotes or spaces and they don't appear in the display. Not sure if that's normal or a clue as to what's wrong.

2
  • 1
    How are you building the html? Commented May 20, 2014 at 0:42
  • Please show the PHP that creates the HTML. It's likely to be there. Commented May 20, 2014 at 0:44

1 Answer 1

3

If you don't want to add any spaces, you should make sure there are none in the textarea when you build the html.

So change:

<textarea class="textarea form_textarea_field" type="textarea" name="comment_1" placeholder="Comments">
            <?php if($saved_form == true) {echo $q1;} ?>
        </textarea>

to:

<textarea class="textarea form_textarea_field" type="textarea" name="comment_1" placeholder="Comments"><?php if($saved_form == true) {echo $q1;} ?></textarea>

Note that everything between the opening and the closing tag of the textarea is part of its content (including new-lines, spaces, etc.).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, try not to indent the code after the openning <textarea></textarea>
Such a beginner's mistake. Thank you so much you guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.