Solved

PHP INSERT Query

Posted on 2014-02-01
10
254 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 109

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 109

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 109

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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