?
Solved

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

Posted on 2010-11-20
5
Medium Priority
?
546 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 1000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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