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')";
brucegustPHP DeveloperAsked:
Who is Participating?
 
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:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
 
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
 
brucegustPHP DeveloperAuthor 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:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.