Solved

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

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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