Solved

PHP INSERT Query

Posted on 2014-02-01
10
253 Views
Last Modified: 2014-02-22
How do you strip commas (,) from an insert query?
Lets say you have an <textarea> and someone writes, "This is why, why not." OR "10,000"

How do you get rid of the comma before insert?
0
Comment
Question by:rgranlund
10 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39826995
$str = 'This is why, why not.';
$str = str_replace(','. '', $str);
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39826997
str_replace("", ",", $input);

HTH,
Dan
0
 
LVL 15

Expert Comment

by:gplana
ID: 39827030
Why do you want to strip the comma? Comma doesn't cause any conflict. Conflict are with single quote.

You should do something like this:

mssql_query("INSERT INTO myTable(field1, field2) VALUES('" . mysql_real_escape_string($my_data) . "', '" . mysql_real_escape_string($my_data2) . "'");

Hope this helps.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39827058
Let's eat kids!
Let's eat, kids!

Commas are important - don't remove them :)

@Marco has a typo in his answer (period instead of comma, ironically). Should be:

$str = str_replace(',', '', $str);

@Dan has the search and replace the wrong way round

@gplana is mixing MSsql with MYsql, and using deprecated functions. Switch to PDO or mySQLi and use prepared statements - quotes are no longer an issue

Sorry guys :)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39827124
+1 for Chris Stanyon's astute observation.

You might want to remove commas in numeric strings (in case you want to store the values as DECIMAL or INT).  In text strings, commas are useful.  Just ask the kids!

Can you please furnish any more of your rationale for wanting to remove commas?  Nothing technical -- just a plain language explanation of the objective.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Author Comment

by:rgranlund
ID: 39830415
I'm trying to create a numeric string.  However, people, no matter what will enter $10,000.00 instead of just 10000
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39830440
OK, that's an easy one.  Strip out the non-numeric parts and cast it to INT.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39830442
Then you should only extract the numbers from the data you are expecting to be numbers. A simple string replace may not be enough here. You probably want to go for a regular expression.

What exactly are you hoping for - just integer (whole) numbers, or are decimals allowed?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39830456
Example using the other script.  See lines 12-23
http://www.laprbass.com/RAY_temp_rgranlund.php?n=$10,000.51

<?php // RAY_temp_rgranlund.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28354169.html

// GET THE NUMBER FROM THE URL ARGUMENT "n="
$num = isset($_GET['n']) ? $_GET['n'] : FALSE;
if ($num === FALSE){
    trigger_error("MISSING URL ARGUMENT n=", E_USER_ERROR);
}

// SANITIZE THE NUMBER
$rgx
= '#'         // REGEX DELIMITER
. '['         // START CHARACTER CLASS
. '^'         // NEGATION - NOTING IN THIS CLASS MATCHES
. '0-9'       // DIGITS
. '.-'        // DECIMAL POINT AND MINUS SIGN
. ']'         // END CHARACTER CLASS
. '#'         // DELIMITER
;
$num = preg_replace($rgx, NULL, $num);
$num = round($num);

// ADJUST THIS FOR YOUR NEEDS
$str = str_pad(number_format($num),8, ' ', STR_PAD_LEFT);

// ADJUST ALL OF THESE FOR YOUR NEEDS, TOO
// CREATE THE IMAGE RESOURCE
$img = imageCreateTrueColor
( 200     // WIDTH
, 60      // HEIGHT
)
;
// WHITE BACKGROUND COLOR
$bgc = imageColorAllocate
( $img   // IMAGE RESOURCE
, 255    // RED
, 255    // GREEN
, 255     // BLUE
)
;
// FIREY RED TEXT COLOR
$txt = imageColorAllocate
( $img   // IMAGE RESOURCE
, 233    // RED
, 14     // GREEN
, 91     // BLUE
)
;
// PAINT THE BACKGROUND
imageFill
( $img   // IMAGE RESOURCE
, 0      // X-OFFSET
, 0      // Y-OFFSET
, $bgc   // FILL COLOR
)
;
// WRITE THE TEXT
imageString
( $img   // IMAGE RESOURCE
, 5      // FONT (SEE THE MAN PAGE)
, 0      // X-OFFSET
, 0      // Y-OFFSET
, $str   // THE STRING DATA
, $txt   // THE TEXT COLOR
)
;
// SEND THE IMAGE INTO THE BROWSER OUTPUT STREAM
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);

Open in new window

0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39830510
Here you go:

$oldStr = '$10,000.00';
$newStr = preg_replace('#[^\d\.-]#', null, $oldStr);
var_dump($newStr);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
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 …

863 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

26 Experts available now in Live!

Get 1:1 Help Now