Jump to content

Problem with quotes?


Mr Chris

Recommended Posts

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'

Link to comment
https://forums.phpfreaks.com/topic/202293-problem-with-quotes/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060745
Share on other sites

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, ..)";

Link to comment
https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060759
Share on other sites

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');

 

Link to comment
https://forums.phpfreaks.com/topic/202293-problem-with-quotes/#findComment-1060761
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.