Jump to content

Data truncated at first space in input box


cotarider

Recommended Posts

I have a page which I want to use to edit records in a MySQL table. The SQL part works fine but when I use echo to put the current values into an input box it only displays the data up to the first space. If as a test I just put echo and the variable name, I get all of the value displayed.

You say the data stores in the SQL table correctly?  In my experience, that usually happens when you don't quote your strings correctly with an INSERT or UPDATE statement (it'll cut off at first space when going into the table...thus it gets only the first part when you display).

You say the data stores in the SQL table correctly?  In my experience, that usually happens when you don't quote your strings correctly with an INSERT or UPDATE statement (it'll cut off at first space when going into the table...thus it gets only the first part when you display).

 

They said if they echo it in another place in the script they get the full value, so I don't think thats the problem.

Erm...

 

<?php

$str = "This is a string";

echo $str;                                    // echo full string

echo " <input type='text' value=$str>";        // truncates

echo " <input type='text' value='$str'>";      // ok

?>

You have to be careful here. If the value of your $str contains a single quote, the above solution will break. To be perfectly safe, use the htmlentities() function with the ENT_QUOTES option:

<?php
echo " <input type='text' value='" . htmlentities($str,ENT_QUOTES) . "'>";
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.