Is there a way to match a urlencoded url within mysql using '=' ? What I am trying to do is check to see if the encoded url already exists in the table.... and if it does don't put it in...if it doesn't then do put it in. For the life of me I can not seem to match $ref if it already exists. $ref = $_SERVER["HTTP_REFERER"]; //encode the url $ref = urlencode($ref); if(mysql_query("SELECT * FROM camefrom where refer='$ref';")) { echo "$ref is in the db"; } else { echo "$ref is NOT in the DB yet so putting it in"; //insert $ref into DB mysql_query("INSERT INTO camefrom(refer) VALUES(\"$ref\" ) ") or die("insert failed"); } Code (markup): Can anyone give me any insight as to what I am doing wrong? Also I am only using urlencode to hopefully make the URL safer before puting it in the database........so if anyone can recommend a better way to do that that would be great too.....as long as I can get it back out as a normal URL too. Thanks in advance!
Use mysql_real_escape_string() instead of urlencode(). And mysql_query() doesn't tell you whether a row exists or not. Do it this way. //encode the url $ref = mysql_real_escape_string($_SERVER["HTTP_REFERER"]); if (mysql_num_rows(mysql_query("SELECT * FROM camefrom where refer='$ref' LIMIT 1;"))) { echo "$ref is in the db"; } else { echo "$ref is NOT in the DB yet so putting it in"; //insert $ref into DB mysql_query("INSERT INTO camefrom(refer) VALUES(\"$ref\" ) ") or die("insert failed"); } PHP:
nico_swd thanks for helping..... it works like a charm...... Now to get it back I am using stripslashes($ref); But that doesn't seem to take out all the slashes. Want I want to do is get the URL back and turn it into a link. What happens if a referrer has some single quotes in it and I turn it into a link with striplashes? Will the links still work if not all the slashes are removed? So far the link seems to work on URL's with a ? in them first....but how can I make it fool proof so that it works every time?
ok I got rid of all the slashes with str_replace() I just wonder if it would be possible for a referering URL to contain single quotes?....