[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Why isn't this number going into my database?

Here's my number: 2,666,672.86

When I do a echo $_POST['current_client_totals'], I get 2,666,672.86. But when I run the code that you see below and then look at what landed in the database, I only see "2." Why? What am I missing?

Here's the code:

$michelle = "INSERT into credibility (student_id, student_name, coaching_call_number, coaching_call_time, rev_goals_current, rev_goals_new, rap_completed, met_pca, prepared, attitude, face_face, rap_review) values ('$_POST[student_id]','$student_name', '$coaching_call_number','$coaching_call_time', '$_POST[current_client_totals]', '$rev_goals_new','$rap_completed','$met_pca',
'$prepared', '$attitude', '$face_face', '$review_rap')";
0
brucegust
Asked:
brucegust
2 Solutions
 
Ray PaseurCommented:
Use a clean numeric string - without the commas!
0
 
Ray PaseurCommented:
Example of how to remove commas...

$num = preg_replace('^0-9\.\-', NULL, $str); // LEAVES ONLY NUMBERS, DECIMAL POINT AND MINUS SIGN

Haven't tested that regex, but I think you can see the principle.

Best regards, ~Ray
0
 
Marco GasiFreelancerCommented:
Check what are the thousand and decimal notation in your locale settings. Probably mysql interprets your number as two units and decimal value and not as 2 millions...

Cheers
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Rik-LeggerCommented:
You should put the type of the database field 'rev_goals_current' to 'double'.
Also discard the comma's in the totals (  str_replace(',', '', '$_POST[current_client_totals]');  )
0
 
brucegustAuthor Commented:
OK. How do I get rid of the commas. The number is coming from a sum computation, but I didn't format the totals so I don't know where the comma is coming from. How do I get rid of it before I try to insert it into the database?
0
 
Rik-LeggerCommented:
$michelle = "INSERT into credibility (student_id, student_name, coaching_call_number, coaching_call_time, rev_goals_current, rev_goals_new, rap_completed, met_pca, prepared, attitude, face_face, rap_review) values ('$_POST[student_id]','$student_name', '$coaching_call_number','$coaching_call_time', str_replace(',', '', '$_POST[current_client_totals]'), '$rev_goals_new','$rap_completed','$met_pca',
'$prepared', '$attitude', '$face_face', '$review_rap')";

Open in new window

0
 
Ray PaseurCommented:
Tested version in the code snippet:
http://www.laprbass.com/RAY_temp_brucegust.php
<?php // RAY_temp_brucegust.php
error_reporting(E_ALL);

// ALL POST DATA ARRIVES IN THE FORM OF A STRING VARIABLE INSIDE THE $_POST ASSOCIATIVE ARRAY
$_POST['current_client_totals'] = '2,666,672.86';

// CONSTRUCT A REGEX TO CLEAN OUR NUMBERS
$rgx
= '#'    // REGEX DELIMITER
. '['    // START CHARACTER CLASS
. '^'    // NEGATION - APPLIES ONLY TO NON-MATCHED CHARACTERS
. '0-9'  // NUMBERS
. '\.'   // ESCAPED DOT
. '\-'   // ESCAPED MINUS SIGN
. ']'    // END CHARACTER CLASS
. '#'    // END REGEX DELIMITER
;

$num = preg_replace($rgx, NULL, $_POST['current_client_totals']);
var_dump($num);

Open in new window

0
 
Ray PaseurCommented:
Sidebar note, but something you will encounter along the way.  Learn about stripslashes(), magic quotes, and mysql_real_escape_string().  You can look these things up on the PHP.net web site.  If you use those correctly (or NOT use them, in the case of magic quotes) your data base will be much more secure and easy to keep right.
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now