Solved

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

Posted on 2010-11-20
5
537 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:bianchef9
  • 2
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 250 total points
ID: 34180902
First I suggest to not use short php tags: use <?php instead.

Second I would suggest to use this:

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


Cheers

0
 
LVL 7

Expert Comment

by:armchang
ID: 34180982
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

0
 

Author Comment

by:bianchef9
ID: 34181771
armchang

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

thanks, but it did not work.
0
 

Author Closing Comment

by:bianchef9
ID: 34181780
Your solution worked perfectly to trim the white spaces both on the form display and on database update.

Thanks.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 34182194
I'm glad to helped you. Good luck with your project.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change password links 7 72
Parts and Products table schema in mysql 6 43
html input type 3 21
MySqlDump not dumping triggers 1 15
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now