Solved

PHP INSERT Query

Posted on 2014-02-01
10
259 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

623 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