Link to home
Start Free TrialLog in
Avatar of bianchef9
bianchef9

asked on

How remove extra spaces at beginning of PHP update form to MySql database

When my PHP form page is launched, an extra white space appears at the beginning of each value from the database (and the database does not contain the extra white space). If I update the form without any change, then the white space in added to the field in the database.

In following updates, the extra space keeps on accumulating so that one white space becomes two white space, etc.

Example
(whitespace) Name <----- is displayed in the PHP update form with Name from the database (No white space is in the database)
then if I update (whether making a change or not)
(whitespace)(whitespace)  Name <----- is displayed in the PHP update form with Name from the database (and the two white white spaces are in the database)

It seems that the white spaces are produced in the PHP or HTML and, of course, inserted into the database so that on several updates display several white spaces before the value Name

How do I remove these white spaces from appearing in the form and sending to the database?
Working code can be viewed below. A solution using my code would be appreciated. Thanks.
html

<tr>
<td>First Name</td>
<td><input type="text" name="first_name" maxlength="50" value="
<?
if(trim($form->value("first_name")) == ""){
   echo $session->userinfo['first_name'];
}else{
   echo $form->value("first_name");
}
?>">
</td>
<td><? echo $form->error("first_name"); ?></td>
</tr>

php

 function updateUserField($username, $field, $value){
      $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
      return mysql_query($q, $this->connection);
   }

and

function setValue($field, $value){
      $this->values[$field] = $value;
	 }
....
   
   function value($field){
	  if(array_key_exists($field,$this->values)){
         return htmlspecialchars(stripslashes($this->values[$field]));
      }else{
         return "";
      }
   }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Why don't you trim returned value before saving into the database, the code will be much simpler:

<tr>
<td>First Name</td>
<td><input type="text" name="first_name" maxlength="50" value="
<?
if(trim($form->value("first_name")) == ""){
   $tmpvalue = trim($session->userinfo['first_name']);
   echo $tmpvalue;
}else{
   $tmpvalue = trim($form->value("first_name"));
   echo $tmpvalue;
}
?>">
</td>
<td><? echo $form->error("first_name"); ?></td>
</tr>

Open in new window

Avatar of bianchef9
bianchef9

ASKER

armchang

I tried your solution but white spaces still were displayed and added on update to the database.

thanks, but it did not work.
Your solution worked perfectly to trim the white spaces both on the form display and on database update.

Thanks.
I'm glad to helped you. Good luck with your project.