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 %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
?>