0

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!

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by peck3277
0

hi

i attach one file of php which will insert value dynamically add the value to database
and html code is given below

<html>
<head>
<center>

<form action="insert.php" method="get">
NAME: <input type="text" name="name" /><br />
NUMBER:<input type="text" name="number" /><br />
<input type="submit" name="submit" />
</center>

</form>

1.file is of html file with two text box and one submit button
and when i click on submit button the text will be inserted into databsetable with using php query ok you see both file properly

2. and in php file i write the code & query of inserting value to db

and another thing it will not workin ie browser
then you try it in opera ok

best of luck

plz see the file properly

Attachments
<?php

$connect=mysql_connect("localhost","root");
$db=mysql_select_db("user");//selecting db
$name=$_GET["name"];//fetch value
$num=$_GET['number'];//fetch value
$in="insert into intro values('$name','$num')";//inserting value
$result=mysql_query("$in")or die(mysql_error());
if($result)
{
echo "inserted";
}
?>
0
value="<?php poem_info($pid,author); ?>" //TO
value="<?php poem_info($pid,'author'); ?>"

AND

$query = 'SELECT '.$record.' FROM poems WHERE id = "'.$pid.'"'; //TO
$query = "SELECT '$record' FROM poems WHERE id = $pid";

Any better?

BTW what DB class you using? Is the db->Record[$record] correct? The function appears a little wasteful with regard including the class. As a rule the db connection vars can be called at the head of the page (or include) and be used throughout - no need to re-instantiate.

Edited by diafol: n/a

0

Hey ardav thanks for the reply. I tired what you said above and it didn't work out, have you got any other idea's?

Here's my mysql class i'm using:

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

Edited by peck3277: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.