Mr Chris Posted May 19, 2010 Share Posted May 19, 2010 Hello People, I am trying to grab the contents of a tab delimited file and put it in a MYSQL table. Now it's working fine apart from one thing. Say I have the tab delimited file i'm grabbing the contents of like so: Timothy O'Brien foreach ( $data as $line ) { $val = trim ($line); $arr = explode ("\t", $val); $val_new = cleanQuotes(implode ("','", $arr)); $query = "INSERT INTO parse_original (player1,player2) VALUES ('".$val_new."')"; mysql_query($query) or die(mysql_error()); } Now the above is grabbing each of the contents like so 'Timothy', 'O'Brien' (With Quotes) and with the above the query will fail becauseof the 'O'Brien' So I have created a function called cleanQuotes that takes any quotes and converts it into into a html version (') But it's now this doing this: 'O'Brien' Can anyone help me keep the function i've written but only run that on the text within the quotes so it parses O'Brien as ? 'O'Brien' Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/ Share on other sites More sharing options...
kenrbnsn Posted May 19, 2010 Share Posted May 19, 2010 Use the function mysql_real_escape_string when you insert the data to escape those characters. Ken Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060735 Share on other sites More sharing options...
Mr Chris Posted May 19, 2010 Author Share Posted May 19, 2010 Thanks , Kinda tried that already using addslashes like mysql_real_escape_string but My query still then comes out as: INSERT INTO parse_original (player1,player2) VALUES (\'Timothy\',\'O\'Brien\'); Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060742 Share on other sites More sharing options...
gwolgamott Posted May 19, 2010 Share Posted May 19, 2010 Also let us see you cleanquote function probrably then could point out where it's adding the extra ' from... but it seems that it's still keeping this 'O'brien' I understand the last one but not sure why it's keeping the first one... either way try using str_replace(find,replace,string,count) a built in function to do this already. Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060745 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 Thanks , Kinda tried that already using addslashes like mysql_real_escape_string but My query still then comes out as: INSERT INTO parse_original (player1,player2) VALUES (\'Timothy\',\'O\'Brien\'); You are passing in your data incorrect, it should be: $data = mysql_real_escape_string($data); $query = "INSERT ... (..., '$data', ..)"; instead of: $data = mysql_real_escape_string("'$data'"); $query = "INSERT ... (..., $data, ..)"; Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060759 Share on other sites More sharing options...
Mr Chris Posted May 19, 2010 Author Share Posted May 19, 2010 Thanks Guys: function cleanQuotes($string) { $replace_characters = array('#',"'"); $html_characters = array("","& #039;"); $cleanstring = str_replace($replace_characters,$html_characters,$string); return $cleanstring; } foreach ( $data as $line ) { $val = trim ($line); $arr = explode ("\t", $val); $val_new = cleanQuotes(implode ("','", $arr)); $query = "INSERT INTO parse_original (player1,player2) VALUES ('".$val_new."')"; print $query; mysql_query($query) or die(mysql_error()); } Outputting as: INSERT INTO parse_original (player1,player2) VALUES ('Timothy','O'Brien'); When it should be: INSERT INTO parse_original (player1,player2) VALUES ('Timothy','O'Brien'); Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060761 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 $val_new = "'" . implode ("','", array_map('cleanQuotes', $arr)) . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060763 Share on other sites More sharing options...
Mr Chris Posted May 19, 2010 Author Share Posted May 19, 2010 $val_new = "'" . implode ("','", array_map('cleanQuotes', $arr)) . "'"; Thanls. but that's insering no values now :-( Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060775 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 Try: $val_new = implode ("','", array_map('cleanQuotes', $arr)); I didn't notice you already included ' in the INSERT query or use: $query = "INSERT INTO parse_original (player1,player2) VALUES ($val_new)"; Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060841 Share on other sites More sharing options...
Mr Chris Posted May 19, 2010 Author Share Posted May 19, 2010 Thanks all Quote Link to comment https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.