Solved

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

Posted on 2010-11-20
5
534 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 30

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 30

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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

19 Experts available now in Live!

Get 1:1 Help Now