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.
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 "";
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
I tried your solution but white spaces still were displayed and added on update to the database.
thanks, but it did not work.
ASKER
Your solution worked perfectly to trim the white spaces both on the form display and on database update.
Thanks.
Thanks.
I'm glad to helped you. Good luck with your project.
Why don't you trim returned value before saving into the database, the code will be much simpler:
Open in new window