Solved

PHP INSERT Query

Posted on 2014-02-01
10
252 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 30

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 42

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
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 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 42

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 42

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

757 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

21 Experts available now in Live!

Get 1:1 Help Now