Solved

PHP strange comma sign

Posted on 2011-02-21
7
335 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 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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 110

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
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 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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

622 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