Solved

PHP INSERT Query

Posted on 2014-02-01
10
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 31

Expert Comment

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

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 110

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

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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
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 count occurrences of each item in an array.

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