Solved

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

Posted on 2010-11-20
5
545 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…
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.

688 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