Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP strange comma sign

Posted on 2011-02-21
7
Medium Priority
?
336 Views
Last Modified: 2012-05-11
Annoying php script which i am trying to figure out.

PHP form with 2 text fields ("phone" and "note") that insert the inputs into a database.
The coder who coded it did not think that the "note" might be left blank as its not a mandatory field.

what happens is that if the user leave the note field blank it prints on the page the phone number and then a comma sign ",".

I am trying to remove the comma sign in case where the note field is left blank.

The original code and my "try-out" on how i tried to manipulate it but did not work below:
 
ORIGINAL CODE:
================================
if($error_phone==0){
		$add_phone = $mysqli_link->prepare("INSERT INTO `vo-company_phones` (`company_id`, `company_phones_phone`, `company_phones_notes`)  VALUES(?, ?, ?)"); 
		$add_phone->bind_param('iss', $company_id, $form_phone, $form_note); 
		$add_phone->execute();
		$add_phone_result=$add_phone->affected_rows; 
		$add_phone->close();
   
  		if($add_phone_result==1){	
			echo "<h3>The phone number was saved successfully.</h3>";
			exit();
		}
		else echo "<h3>An error occured.</h3>";
	}	
}

MY EDITED CODE:
===============================================
if($error_phone==0){
		if (strlen($form_note)<2) {	
		$add_phone = $mysqli_link->prepare("INSERT INTO `vo-company_phones` (`company_id`, `company_phones_phone`)  VALUES(?,?)");	
		$add_phone->bind_param('is', $company_id, $form_phone);
		$add_phone->execute();
		$add_phone_result=$add_phone->affected_rows; 
		$add_phone->close();
		echo "num out";
   
  		if($add_phone_result==1){	
			echo "<h3>The phone number was saved successfully.</h3>";
			exit();
		}
		else echo "<h3>An error occured.</h3>";
	}
	}
		if (strlen($form_note)>2) {
		$add_phone = $mysqli_link->prepare("INSERT INTO `vo-company_phones` (`company_id`, `company_phones_phone`, `company_phones_notes`)  VALUES(?, ?, ?)"); 
		$add_phone->bind_param('iss', $company_id, $form_phone, $form_note);
		}
		$add_phone->execute();
		$add_phone_result=$add_phone->affected_rows; 
		$add_phone->close();
		echo "num in";
   
  		if($add_phone_result==1){	
			echo "<h3>The phone number was saved successfully.</h3>";
			exit();
		}
		else echo "<h3>An error occured.</h3>";
	}

Open in new window

0
Comment
Question by:Refael
[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
  • 2
  • +1
7 Comments
 
LVL 17

Accepted Solution

by:
Shinesh Premrajan earned 800 total points
ID: 34948670
if (strlen($form_note)<2)

should be

if(strpos(",",$form_note)==0)

Hope this helps
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34948672
How about this:

if($error_phone==0){

	if ( $form_note != '' ){
		$add_phone = $mysqli_link->prepare("INSERT INTO `vo-company_phones` (`company_id`, `company_phones_phone`, `company_phones_notes`)  VALUES(?, ?, ?)"); 
		$add_phone->bind_param('iss', $company_id, $form_phone, $form_note); 
	}
	else {
		$add_phone = $mysqli_link->prepare("INSERT INTO `vo-company_phones` (`company_id`, `company_phones_phone`)  VALUES(?, ?)"); 
		$add_phone->bind_param('is', $company_id, $form_phone); 
	}
	$add_phone->execute();
	$add_phone_result=$add_phone->affected_rows; 
	$add_phone->close();

	if($add_phone_result==1){	
		echo "<h3>The phone number was saved successfully.</h3>";
		exit();
	}
	else echo "<h3>An error occured.</h3>";
	}	
}

Open in new window

0
 

Author Comment

by:Refael
ID: 34948733

Hi shinuq, Hi sjklein42,

I have tested both solutions. they both still print the comma "," after the phone number.
and this "if(strpos(",",$form_note)==0)" prints an error.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 800 total points
ID: 34948742
When you say it prints the comma, is that in some report that you aren't showing us here?

I suspect the problem is in the code that formats of the report, not in the loading of the database.
0
 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 34949042
if(strpos(",",$form_note)==0)

This is a valid code, it seems that the variable  $form_note is not initialized in the PHP code, that resulted in the error.


Hope this helps.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 34951040
I am not sure I understand all the issues here, but I can give you some of the theory.  When you construct a string for display in the browser and you want to separate the fields with commas, you can use trim() and empty() to determine whether the field contains meaningful information.  Then you can add commas if they are appropriate.  Install this and run it to see the moving parts, or just test on my web site here.
http://www.laprbass.com/RAY_temp_refe.php
<?php // RAY_temp_refe.php
error_reporting(E_ALL);

// TEST DATA COMES FROM THE FORM INPUT
if (!empty($_POST))
{
    // TRIM WHITESPACE FROM THE POSTED DATA
    $f1 = trim($_POST["f1"]);
    $f2 = trim($_POST["f2"]);

    // CREATE THE OUTPUT STRING
    $out = $f1;

    // IF THE SECOND FIELD IS PRESENT, WE NEED TO ADD A COMMA BEFORE IT
    if ($f2 != '')
    {
        $out .= ',';
        $out .= $f2;
    }

    // WE DO NOT WANT COMMAS ON THE BEGINNING OR END OF THE STRING
    $out = trim($out, ',');

    // SHOW THE WORK PRODUCT
    if (!empty($out)) echo "WORK PRODUCT IS " . htmlentities($out);
}

// CREATE THE FORM IN HEREDOC NOTATION
$form = <<<FORM
<form method="post">
ENTER ZERO, ONE OR TWO STRINGS
<input name="f1" />
<input name="f2" />
<input type="submit" />
</form>
FORM;

echo $form;

Open in new window

0
 

Author Comment

by:Refael
ID: 34952113

sjklein42 you did it :-)
i was so occupied in the form page instead of looking at the page that display the information back from the database. enlighten me!

all the solutions above are excellent i was just looking at the wrong page.
Thank guys!
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

722 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