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
Solved

PHP strange comma sign

Posted on 2011-02-21
7
329 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 17

Accepted Solution

by:
Shinesh Premrajan earned 200 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 200 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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 48
error while installing php56 in redhat enterprise linux 20 50
ajax to record click 3 16
Formating field inside mysql query 2 10
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 …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

792 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