refiking Posted August 14, 2007 Share Posted August 14, 2007 I am uploading a csv file into mysql, but I need to enter the telephone number without the hyphens. I basically need the telephone number to be stored in mysql db like this xxxxxxxxx instead of xxx-xxx-xxxx. What can I change to fix that? Here is the code: <?php include "connect.php"; if(isset($_POST['submit'])) { $filename=$_POST['filename']; $handle = fopen("$filename", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import="INSERT into prospects(Sal,BFN,BLN,Addy,City,State,Zip,Value,Tenure,Phone ) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')"; mysql_query($import) or die(mysql_error()); } fclose($handle); print "Import done"; } else { print "<form action='import.php' method='post'>"; print "Type file name to import:<br>"; print "<input type='text' name='filename' size='20'><br>"; print "<input type='submit' name='submit' value='submit'></form>"; } ?> Link to comment https://forums.phpfreaks.com/topic/64904-solved-reformatting-telephone-numbers/ Share on other sites More sharing options...
lemmin Posted August 14, 2007 Share Posted August 14, 2007 ereg_replace("-", null, $phonenumber); That should do it. Link to comment https://forums.phpfreaks.com/topic/64904-solved-reformatting-telephone-numbers/#findComment-323820 Share on other sites More sharing options...
refiking Posted August 14, 2007 Author Share Posted August 14, 2007 where exactly does that go? Link to comment https://forums.phpfreaks.com/topic/64904-solved-reformatting-telephone-numbers/#findComment-323827 Share on other sites More sharing options...
lemmin Posted August 14, 2007 Share Posted August 14, 2007 You can use it right in the sql statement, if you want. $import="INSERT into prospects(Sal,BFN,BLN,Addy,City,State,Zip,Value,Tenure,Phone ) values('" . ereg_replace("-", null, $data[0]) . "','$data[1]','$data[2]','$data... That is, assuming you wanted to remove "-" from $data[0]. Link to comment https://forums.phpfreaks.com/topic/64904-solved-reformatting-telephone-numbers/#findComment-323834 Share on other sites More sharing options...
refiking Posted August 14, 2007 Author Share Posted August 14, 2007 No, I'd like to remove it from data[9] So, should it be like this? $import="INSERT into prospects(Sal,BFN,BLN,Addy,City,State,Zip,Value,Tenure,Phone ) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]',ereg_replace("-", null, $data[9]')"; Link to comment https://forums.phpfreaks.com/topic/64904-solved-reformatting-telephone-numbers/#findComment-323852 Share on other sites More sharing options...
leesiulung Posted August 14, 2007 Share Posted August 14, 2007 To make the code more robust, I would strip anything out that isn't a digit even if your number is guaranteed to be of that format. People do dumb things sometimes. Might take a small peformance penalty, but how often do you do inserts? Hopefully not as often as selects.... Link to comment https://forums.phpfreaks.com/topic/64904-solved-reformatting-telephone-numbers/#findComment-323860 Share on other sites More sharing options...
refiking Posted August 14, 2007 Author Share Posted August 14, 2007 Ok. That code worked!!! But........... Now, every record has the same exact telephone number appearing in the database. How can I fix this? Link to comment https://forums.phpfreaks.com/topic/64904-solved-reformatting-telephone-numbers/#findComment-323883 Share on other sites More sharing options...
refiking Posted August 14, 2007 Author Share Posted August 14, 2007 Fixed it! The problem was I set it up as an integer instead of a varchar. Now, it's working perfectly! Thanks! Link to comment https://forums.phpfreaks.com/topic/64904-solved-reformatting-telephone-numbers/#findComment-323892 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.