Dynamically filled form inserts space at beginning of the field

Hi guys,

I have a form where users update a posts information.

One of the sections is the following :

<td><input type="text" name="author" size="30" value="<?php poem_info($pid,author); ?>"></td>

as you can see I’m using php to populate the field. Here is the function poem_info():

function poem_info($pid,$record){
		require_once '../classes.db_connect.php';
		$db = new Database;
 
		$query = 'SELECT '.$record.' FROM poems WHERE id = "'.$pid.'"';
		$db->query($query);
 
		$db->singleRecord();    // retrieve a single record
		echo $db->Record[$record];
	}

The problem I am having is that if I edit the information in the form and save it to the database it saves perfectly. If I check the database it’s saved with no spaces before the user input.

But when I enter the form to edit the information a space is inserted and I can’t figure out why. I have tried to use trim() but it’s still not working and it’s driving me mad. I know it’s not the DB because when I log in and check it manually it’s fine. So I must be doing something wrong somewhere else and it’s killing me! Please help!

also the database class im using is:

<?
class Database
    {
    var $Host     = ""; // Hostname of our MySQL server.
    var $Database = ""; // Logical database name on that server.
    var $User     = ""; // User and Password for login.
    var $Password = "";
    
    var $Link_ID  = 0;  // Result of mysql_connect().
    var $Query_ID = 0;  // Result of most recent mysql_query().
    var $Record   = array();  // current mysql_fetch_array()-result.
    var $Row;           // current row number.
    var $LoginError = "";
	
	var $sanatized;		//sanatized entry

    var $Errno    = 0;  // error state of query...
    var $Error    = "";
	
	/*------------------------------------------------------------
	                       Example Usage
	--------------------------------------------------------------
	include ('db_connect.php');
	
	$db = new Database;
	
	// do a query to retrieve a single record
	$query = "SELECT * FROM members LIMIT 1";
	$db->query($query);        // query the database
	$db->singleRecord();    // retrieve a single record
	echo $db->Record['username'].'<br>';        // output a field value from the recordset

	// do a query to retrieve multiple records
	$query = "SELECT * FROM members";
	$db->query($query);        // query the database
	while ($db->nextRecord())
	{
	echo $db->Record['username']."<br>";        // output a field value from the recordset
	} // end while loop going through whole recordset

	// do a query to count number of rows
	$query = "SELECT * FROM members";
	$db->query($query);
	echo $db->numRows();
	
	*/
    
//-------------------------------------------
//    Connects to the database
//-------------------------------------------
    function connect()
        {
        if( 0 == $this->Link_ID )
            $this->Link_ID=mysql_connect( $this->Host, $this->User, $this->Password );
        if( !$this->Link_ID )
            $this->halt( "Link-ID == false, connect failed" );
        if( !mysql_query( sprintf( "use &#37;s", $this->Database ), $this->Link_ID ) )
            $this->halt( "cannot use database ".$this->Database );
        } // end function connect

//-------------------------------------------
//    Queries the database
//-------------------------------------------
    function query( $Query_String )
        {
        $this->connect();
		/*$this->sanatized = mysql_real_escape_string($Query_String);
		$this->sanatized = stripslashes($this->sanatized);
        $this->Query_ID = mysql_query( $this->sanatized,$this->Link_ID );*/
        $this->Query_ID = mysql_query( $Query_String,$this->Link_ID );
        $this->Row = 0;
        $this->Errno = mysql_errno();
        $this->Error = mysql_error();
        if( !$this->Query_ID )
            $this->halt( "Invalid SQL: ".$Query_String );
        return $this->Query_ID;
        } // end function query

//-------------------------------------------
//    If error, halts the program
//-------------------------------------------
    function halt( $msg )
        {
        printf( "</td></tr></table><b>Database error:</b> %s<br>", $msg );
        printf( "<b>MySQL Error</b>: %s (%s)<br>", $this->Errno, $this->Error );
        die( "Session halted." );
        } // end function halt

//-------------------------------------------
//    Retrieves the next record in a recordset
//-------------------------------------------
    function nextRecord()
        {
        @ $this->Record = mysql_fetch_array( $this->Query_ID );
        $this->Row += 1;
        $this->Errno = mysql_errno();
        $this->Error = mysql_error();
        $stat = is_array( $this->Record );
        if( !$stat )
            {
            @ mysql_free_result( $this->Query_ID );
            $this->Query_ID = 0;
            }
        return $stat;
        } // end function nextRecord

//-------------------------------------------
//    Retrieves a single record
//-------------------------------------------
    function singleRecord()
        {
        $this->Record = mysql_fetch_array( $this->Query_ID );
        $stat = is_array( $this->Record );
        return $stat;
        } // end function singleRecord

//-------------------------------------------
//    Returns the number of rows  in a recordset
//-------------------------------------------
    function numRows()
        {
        return mysql_num_rows( $this->Query_ID );
        } // end function numRows
        
    } // end class Database
?> 

Hey Avenir, I’m after trying that but still no luck.

I’m after putting echoing that function in a few other places around the page. Whenever it meets poem_info() for the first time it inserts a space. Everytime it meets poem_info after that it displays correctly with no spaces.

I imagine I am doing something small and stupid but i just can’t spot where!

very strange, maybe you could try this even if you already use trim():

In your poem_info function, replace the echo (last line) by return and in your html code:


<td><input type="text" name="author" size="30" value="<?php echo trim(poem_info($pid,'author')); ?>"></td>

php code seems ok. Are you using CSS? maybe there is a style defined for the text input?

It’s not css anyways. I just tried changing this:

<td><input type="text" name="author" size="30" value="<?php poem_info($pid,author); ?>"></td>

To:

<td><input type="text" name="author" size="30" value="<?php echo 'hi'; ?>"></td>

And in the input box there is no space at the beginning. So that leave it to be either the DB or the poem_info() function. When i look at the DB I can’t see any space at the beginning.