Solved

PHP strange comma sign

Posted on 2011-02-21
7
320 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:
shinuq 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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:shinuq
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 108

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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…
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…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now