Solved

PHP strange comma sign

Posted on 2011-02-21
7
331 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 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